How to use `git merge` to achieve same result as `git pull`?
What is the difference between 'git pull' and 'git fetch'?
The question above's top answer says
"In the simplest terms, git pull does a git fetch followed by git merge or git rebase."
I understand that git fetch && git rebase
is literally git pull
.
But how to use git fetch
and git merge
to achieve the same result?
Here is my experiment:
git fetch && git rebase # correctly replays remote commits in working copy
git fetch && git merge upstream/master # error
merge: upstream/master - not something we can merge
I would expect git merge
to be able to correctly replay remote commits in the current working copy - which is what git pull
does.
You should use git fetch + git merge
like git fetch; git merge origin/master
git fetch; git merge origin/master
to achive same behaviour as default behaviour of git pull
. But default behaviour can be changed to use rebase.
More about differences between merge and rebase are here: git pull VS git fetch git rebase
git fetch
this command wil download the status of the remote repository and its branches but will do nothing with your local copy
git merge branch_to_merge
this command will merge the branch_to_merge in the branch you currently are. You cannot just type git merge without parameters because git will reply with: fatal: No commit specified and merge.defaultToUpstream not set
git rebase
This command will let you rewrite the history of your branch and put it over the specified commit
链接地址: http://www.djcxy.com/p/45124.html