0

I am trying to understand when to use get fetch instead of git pull. I have read before (such as here and here) that the difference between git pull and get fetch is that pull does a fetch followed by a merge. That is all well and good except…wait - what does fetch actually do if it does not merge? What does it even mean to fetch updates from the remote repository if you do not merge into the local repository?

For example, say my local repository is three commits behind and one commit ahead of the remote repository. If I do a git pull, I will see those three commits in my local repository, with the possibility of “<<<<<<< HEAD” in some files if there is a merge conflict. The git log would would have an extra commit for the pull.

If I do a fetch, would not those three commits show up in my local repository? If they do, how is it effectively different than the pull? Does it handle conflicts differently than the pull or is there some other difference?

Community
  • 1
  • 1
Paul J Abernathy
  • 993
  • 2
  • 12
  • 25
  • 2
    Possible duplicate of [What are the differences between 'git pull' and 'git fetch'?](http://stackoverflow.com/questions/292357/what-are-the-differences-between-git-pull-and-git-fetch) – Makoto Oct 05 '15 at 20:30

1 Answers1

1

Your git repository stores data about local and remote branches separately. When you git push or git pull, git typically will link your local branch (say develop) with the remote branch (say origin/develop). If you do a git fetch, it just updates the local copy of your remote branch. You can see this by doing git checkout origin/develop. If you want to subsequently update your local branch, run git merge origin/develop.

TL;DR; git pull ~= git fetch + git merge

Sam
  • 20,096
  • 2
  • 45
  • 71
  • Interesting. I think this filled in the missing link in my understanding fetch/pull. Thanks. – Paul J Abernathy Oct 05 '15 at 20:57
  • Here's [further reading](https://git-scm.com/book/en/v2/Git-Internals-Git-References) on git references (the stuff that stores information about what commit your branches, tags, etc. point to). – Sam Oct 05 '15 at 21:31