How to sync/update forked git repository with upstream?


I found the origin post in Chinese is more popular among all the posts, so just rewrite it in English now :)

Once you forked a repository on GitHub, the commit history from the upstream will stay at the moment you forked it, sometimes we’ll need to update it, so that the parent of our commit won’t be so old, which can help us have a better look, easier to trace git commit history, and prevent some potential or known conflicts, maybe you have other reasons, anyway. This method can also sync a repository between different git servers.

If you forked repository is behind the upstream repository for more than 1k commits and the repository is fat, you can consider to delete your forked repository and re-fork the origin one, it may be faster and more efficient.

Let’s start it.

First, if you didn’t have the repository locally, you have to clone the forked repository to local, you can set the clone depth to save the bandwidth and disk space usage:
$ git clone --depth 1

(Replace the url to your forked repository)

Once you cloned it, check its ‘remote’, usually you’ll get only one remote after clone, like this:
$ git remote -v
origin (fetch)
origin (push)

Now we should add another ‘remote’ – the origin upstream, so that we can pull the updates from, in this case, use read-only git protocol will be faster, more efficient (but note that some firewall may not allow that protocol, so you’ll need to use https in that case):
$ git remote add upstream git://

PS: ‘upstream’ is the name I gave it, you can give it another name as you want.

To verify new added remote, let’s check it again, you should have two remotes now:

origin (fetch)
origin (push)
upstream git:// (fetch)
upstream git:// (push)

Now we can start the “update” works, I assume the branch you’re going to update is the master branch, if you are going to update a non-master branch, just checkout to the branch you want, but don’t forget to change the branch from the below examples!

If your branch is only behind the upstream, no any “ahead” commits(which means you didn’t commit any new things on the same branch came from upstream), you can directly pull the updates from upstream:
$ git pull upstream master

If your branch also contains your own commits, you should better pull with “–rebase” parameter:
$ git pull --rebase upstream master

Now, almost done, if there is no error or conflicts(we don’t discuss conflicts here), push your master to origin remote, then you’ll found that your forked repo is fresh again:
$ git push origin master

更新從 GitHub 上 fork 出來的 repository (或是同步兩個不同 server 端的 repository)

GitHub 上面 fork 出來的 repository,整個狀態會停留在當初 fork 的時候,後面的同步要靠自己動手,當然你也可以把 fork 出來的 repository 整個砍掉重新 fork,這邊要講的是手動同步、而不用砍掉重練的方法,有兩個不同的 git server 要做同步的動作也是這樣做,有些專案會同時丟在自己的 git server 跟 GitHub 上,就會需要用到

大致講一下流程 … 操作必須是在自己 local 端做 (希望以後 GitHub 可以推出按一個鈕就幫我去同步到最新XD),做法是把 fork 出來的 repository 在本地更新後、再把 repository 給 push 出去!會需要用到 git 的 branch, remote, pull, push 觀念。

接下來的操作環境是你已經把你 fork 出來的 repository 給 clone 到自己的電腦上了, clone 好之後請切換到對應的資料夾底下,然後就可以開始使用 git 做操作了。

第一次操作時我們要加入一個遠端的 remote 當作更新來源,如果要比較是否有加入成功的話可以在操作前後先看狀態:
$ git remote -v

預設應該只會有 origin 這個 remote:
origin (fetch)
origin (push)

我們用下面這個命令來加入遠端的 repository,在這邊的情境也就是比較新的、上游的 repository
upstream 是 remote name、可以自己取名,不要重複就好,但後面我都用會 upstream 做示範
而後面那串網址是 repository 位置:
$ git remote add upstream

如果再看一次現有的remote端應該會發現多了兩組 upstream (fetch & push):
$ git remote -v
origin (fetch)
origin (push)
upstream (fetch)
upstream (push)


假如我要做更新的的 branch 是 master 的話就先切到 master branch:
$ git checkout master

接著把 upstream 的 master 更新給拉進來
$ git pull upstream master

如果你的 master branch 有自己的 commit, 也可以用 rebase 來避免不必要的 merge 操作:
$ git pull --rebase upstream master

如果沒有發生衝突的話這樣應該就完成了本地的更新,再把更新後的 branch push 出去就行了
$ git push origin master

這時再回去看看你的 repository 頁面,應該會發現已經同步到最新的狀態了!