Fork 他人的仓库后,原作者又更新了仓库,此时如何将自己仓库的代码跟原仓库保持一致呢?下面将给出解答。
为 Fork 配置远程仓库
列出当前为 Fork 配置的远程仓库。
1
2
3$ git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)指定将与 Fork 同步的新远程上游(
upstream
)仓库。1
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
验证为 Fork 指定的新上游仓库。
1
2
3
4
5$ git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
同步 Fork
从上游仓库获取分支及其各自的提交,传送到本地,对
master
分支的提交将存储在本地分支upstream/master
中。1
2
3
4
5
6
7$ git fetch upstream
remote: Counting objects: 75, done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 62 (delta 27), reused 44 (delta 9)
Unpacking objects: 100% (62/62), done.
From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master切换到本地的主分支。
1
2$ git checkout master
Switched to branch 'master'将来自
upstream/master
的更改合并到本地master
分支中。这就实现了与上游仓库的同步,而不会丢失本地的更改。1
2
3
4
5
6
7
8$ git merge upstream/master
Updating a422352..5fdff0f
Fast-forward
README | 9 -------
README.md | 7 ++++++
2 files changed, 7 insertions(+), 9 deletions(-)
delete mode 100644 README
create mode 100644 README.md最后推送到远程仓库就完成了。
1
$ git push origin master