Syncing fork with upstream: git fetch + git checkout + git merge vs. git checkout + git pull

微笑、不失礼 提交于 2020-08-10 18:21:47

问题


The documentation at Github-Help: Syncing a Fork shows three commands to keep my GitHub fork in sync with the upstream repo.

git fetch upstream
git checkout master
git merge upstream/master

Can I use the following two commands instead of the above three?

git checkout master
git pull upstream/master

Are the two sets of commands equivalent, or are there differences between them?


回答1:


These command sets are not equivalent.

git pull

is split into two commands:

git fetch
git merge

The problem is, that git fetch requires a remote reference, while git merge requires a tracking reference, this is why the Github help page has:

git fetch upstream

but it has

git merge upstream/master

The merge command will take the upstream/master branch and merge it into the currently checked out branch (in this case 'master'). But the fetch command doesn't work on a branch, it requires a remote, so when you try:

git pull upstream/master

Git splits this into:

git fetch upstream/master
git merge upstream/master

which will fail on the fetch:

$ git pull upstream/master
fatal: 'upstream/master' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


来源:https://stackoverflow.com/questions/40602721/syncing-fork-with-upstream-git-fetch-git-checkout-git-merge-vs-git-checkou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!