同步一個 fork

fork 了別人的倉庫後,原做者又更新了倉庫,如何將本身的代碼和原倉庫保持一致?本文將給你解答。git

如何使用搜索引擎

其實這個問題並不難,我又被坑了。百度搜的東西不靠譜啊,之後這種問題必定要用英文在 Google 或者 Bing 上搜索,這樣才能搜到原汁原味的答案。就當是一個教訓吧。github

搜索 fork sync,就能夠看到 GitHub 本身的幫助文檔 Syncing a fork 點進去看這篇的時候,注意到有一個 Tip: Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git.fetch

根據這兩篇文章,問題迎刃而解!搜索引擎

具體方法

Configuring a remote for a fork

給 fork 配置一個 remotegoogle

主要使用 git remote -v 查看遠程狀態。spa

git remote -v # origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
# origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)

添加一個將被同步給 fork 遠程的上游倉庫code

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

再次查看狀態確認是否配置成功。blog

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)

Syncing a fork

從上游倉庫 fetch 分支和提交點,傳送到本地,並會被存儲在一個本地分支 upstream/master
git fetch upstream索引

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

切換到本地主分支(若是不在的話)
git checkout masterip

git checkout master # Switched to branch 'master'

把 upstream/master 分支合併到本地 master 上,這樣就完成了同步,而且不會丟掉本地修改的內容。 
git merge upstream/master

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

若是想更新到 GitHub 的 fork 上,直接git push origin master就行了。

Ref

https://gaohaoyang.github.io/2015/04/12/Syncing-a-fork/

相關文章
相關標籤/搜索