3

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.

Community
  • 1
  • 1
  • 2
    Which result are you talking about? It already produces the same end result (because `git pull` is nothing more than a shell script calling fetch, then merge – you can even look at its source code) – knittl Nov 21 '13 at 09:10
  • 1
    As stated in your quote, basically the result is the same; anyway it looks like you have no clear idea about the differences between rebase and merge – Lorenzo Marcon Nov 21 '13 at 09:12
  • It simply does not work. `git fetch && git rebase` correctly replays upstream changes in my working copy, `git fetch && git merge` does not do anything and gives an error "not something we can merge". –  Nov 21 '13 at 09:12
  • @HowardGuo you cannot simply type `git merge`, you need to specify which branchs do you want to merge – iberbeu Nov 21 '13 at 09:14
  • @iberbeu I did `git merge upstream/master`. without upstream, git merge complains other error. –  Nov 21 '13 at 09:16
  • `git merge` does never replay your local commits. It either creates a new merge commit or fast-forwards your current branch. – knittl Nov 21 '13 at 09:28

2 Answers2

1
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

iberbeu
  • 15,295
  • 5
  • 27
  • 48
  • Then how to git merge "remote repository status" (master branch) with the master branch working copy? –  Nov 21 '13 at 09:19
  • 1
    got to local master `git checkout master` and then merge the status of the remote copy (let say origin) `git merge origin/master`. All this of course after doing `git fetch origin` – iberbeu Nov 21 '13 at 09:21
0

You should use git fetch + git merge like 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

Community
  • 1
  • 1
klm123
  • 12,105
  • 14
  • 57
  • 95
  • `git pull` does not change its behavior depending on whether you have changes or not. It will use merge, unless you call it as `git pull --rebase`. – knittl Nov 21 '13 at 09:29