Github上fork專案後保持與源專案更新

dinghb發表於2017-02-19

背景

在GitHub上fork了官方的一個專案,想要將官方專案的commit提交記錄同步到自己fork的專案中。

例如:

  • 官方專案的源遠端倉庫:git@github.com:openstack/manila.git

  • 個人fork專案的遠端倉庫:git@github.com:dinghb/manila.git

此時,想要將官方專案的源遠端倉庫上面的commits記錄,同步到個人fork專案的遠端倉庫中。

同步源專案步驟

fork了官方源遠端倉庫後,使用git clone <個人遠端倉庫> 建立本地master分支

$ git clone git@github.com:dinghb/manila.git
Cloning into `manila`...
remote: Counting objects: 34192, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 34192 (delta 20), reused 9 (delta 9), pack-reused 34160
Receiving objects: 100% (34192/34192), 10.59 MiB | 1.03 MiB/s, done.
Resolving deltas: 100% (25063/25063), done.
Checking connectivity... done.

此時檢視列舉本地master分支的個人遠端倉庫,如下只有一個預設的orgin倉庫。

$ git remote -v
origin    git@github.com:dinghb/manila.git (fetch)
origin    git@github.com:dinghb/manila.git (push)

保持fork後個人專案和源專案的更新同步的關鍵步驟就在於本地分支關聯官方源遠端倉庫,使用遠端倉庫的地址git@github.com:dinghb/manila.git,並設定一個別名。

$ git remote add openstackmanila git@github.com:openstack/manila.git
$ git remote -v
openstackmanila    git@github.com:openstack/manila.git (fetch)
openstackmanila    git@github.com:openstack/manila.git (push)
origin    git@github.com:dinghb/manila.git (fetch)
origin    git@github.com:dinghb/manila.git (push)

將源遠端倉庫專案pull到本地master分支(此處也可以先git fetch源倉庫提交記錄到本地,然後在git merge或者git rebase)

$ git pull openstackmanila master
remote: Counting objects: 3821, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 3821 (delta 1864), reused 1859 (delta 1859), pack-reused 1943
Receiving objects: 100% (3821/3821), 1.37 MiB | 324.00 KiB/s, done.
Resolving deltas: 100% (2917/2917), completed with 455 local objects.
From github.com:openstack/manila
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> openstackmanila/master
Updating ac2d20b..741136e
Fast-forward
...

此時本地master分支便可提交commits到遠端個人分支(origin倉庫),即儲存了源官方專案與個人fork專案的程式碼同步。

$ git push origin
Counting objects: 3821, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1286/1286), done.
Writing objects: 100% (3821/3821), 1.06 MiB | 0 bytes/s, done.
Total 3821 (delta 2848), reused 3386 (delta 2505)
remote: Resolving deltas: 100% (2848/2848), completed with 320 local objects.
To github.com:dinghb/manila.git
   ac2d20b..741136e  master -> master

相關文章