天天看點

Github上fork項目更新

場景:很早之前fork了github/gitee/gitlab等等的項目,但是項目經過多次疊代送出了更多更新,而自己賬号下面被fork過來的項目并沒有及時更新。此時,需要把送出過的内容更新到自己賬号下的遠端分支上。以我自己fork過的一個項目為例,主要分添加上遊倉庫和同步兩個步驟。

添加上遊倉庫

檢視遠端分支狀态:

git remote -v
           
Github上fork項目更新

添加需要同步的上遊倉庫:

git remote add upstream https://github.com/tywo45/t-io
           

再次檢視遠端狀态:

$ git remote -v
origin  [email protected]:eEricZeng/t-io.git (fetch)
origin  [email protected]:eEricZeng/t-io.git (push)
upstream        https://github.com/tywo45/t-io (fetch)
upstream        https://github.com/tywo45/t-io (push)
           

同步

将上遊倉庫内容拉到本地:

git fetch upstream
           

此時,并沒有将上遊倉庫的内容合并到本地中,可以使用status檢視,也可以對比更新:

git diff master upstream/master --stat
           

若要同步哪個分支就切換到對應的分支,例如master分支:

git checkout master
           

同步并推送到自己賬号下的遠端分支,這裡的同步本質是把upstream中master分支的内容合并到目前的master分支中:

git merge upstream/master
git push origin master