Github上fork项目与上游同步


Github上fork别人的项目之后有时候希望与上游的源码保持一致,同时远程分支也需要进行维护。

克隆项目到本地

虽然github也提供在线编辑功能,但是一般还是拉回本地更方便。这里以weld-core项目为例:

[edony@edony coding:167]$ git clone git@github.com:edonyM/python-patterns.git
Cloning into 'python-patterns'...
remote: Counting objects: 636, done.
remote: Total 636 (delta 0), reused 0 (delta 0), pack-reused 636
Receiving objects: 100% (636/636), 134.75 KiB | 29.00 KiB/s, done.
Resolving deltas: 100% (381/381), done.

设置上游远程仓库

当成功fork一个项目后,无论你怎么修改fork出来的项目,原来的项目(github叫做upstream,一般译作上游项目)是不会受到影响的,这在上游项目来说自然是极好的保护,但是,fork出来的项目如何能够及时反映上游项目的变更呢?这就首先需要设置本地项目的“远程仓库”属性,即告诉git命令,本地项目的上游项目是谁。比如针对weld-core项目,

git remote add upstream https://github.com/faif/python-patterns.git

设置好远程仓库后,可以检查一下:

[edony@edony python-patterns:172]$ git remote -v
origin git@github.com:edonyM/python-patterns.git (fetch)
origin git@github.com:edonyM/python-patterns.git (push)
upstream https://github.com/faif/python-patterns.git (fetch)
upstream https://github.com/faif/python-patterns.git (push)

现在,本地仓库有了两个远程仓库:

  1. origin: 这是fork出来的仓库
  2. upstream: 这是上游项目的仓库

同步远程仓库

设置好上游远程仓库后,将上游远程仓库的最新变化更新到本地。我这边的情况是很久没有和上游项目同步了,因此更新下来的内容比较多:

[edony@edony python-patterns:173]$ git fetch upstream
remote: Counting objects: 614, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 614 (delta 158), reused 155 (delta 155), pack-reused 440
Receiving objects: 100% (614/614), 217.50 KiB | 75.00 KiB/s, done.
Resolving deltas: 100% (353/353), completed with 31 local objects.
From https://github.com/faif/python-patterns
 * [new branch] master -> upstream/master

设置本地分支

首先查看一下当前有哪些分支:

[edony@edony python-patterns:174]$ git branch -a
* master
 remotes/origin/HEAD -> origin/master
 remotes/origin/master
 remotes/upstream/master

要确保本地代码的分支(branch)设置合适,一般设置为master:

git checkout master

合并上游项目和本地项目

既然本地分支设置为master,有必要将上游项目的master和本地的master合并,也就是上游项目的最新变化融合到本地:

[edony@edony python-patterns:175]$ git merge upstream/master
Updating 18a6cd2..8414931
Fast-forward
 .travis.yml | 34 ++++++++++++++++++
 README.md | 90 ++++++++++++++++++++++++++++++-----------------
 behavioral/__init__.py | 0
 behavioral/catalog.py | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 chain.py => behavioral/chain.py | 51 ++++++++++++++++++---------
 chaining_method.py => behavioral/chaining_method.py | 4 +++
 behavioral/command.py | 57 ++++++++++++++++++++++++++++++
 iterator.py => behavioral/iterator.py | 8 ++---
 mediator.py => behavioral/mediator.py | 1 -
 memento.py => behavioral/memento.py | 16 +++++----

提交本地变更,更新origin仓库

本地的变更需要git push回origin:

[edony@edony python-patterns:176]$ git push origin master
Counting objects: 614, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (287/287), done.
Writing objects: 100% (614/614), 218.72 KiB | 0 bytes/s, done.
Total 614 (delta 341), reused 586 (delta 326)
remote: Resolving deltas: 100% (341/341), completed with 14 local objects.
To github.com:edonyM/python-patterns.git
 18a6cd2..8414931 master -> master
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s