问题
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