背景
在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