在GitHub上fork了官方的一個項目,想要將官方項目的commit提交記錄同步到本身fork的項目中。git
例如:github
此時,想要將官方項目的源遠程倉庫上面的commits記錄,同步到我的fork項目的遠程倉庫中。shell
fork了官方源遠程倉庫後,使用git clone <我的遠程倉庫> 建立本地master分支fetch
$ 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倉庫。code
$ 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,並設置一個別名。rem
$ 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)get
$ 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