The Difference Between ‘Git Fetch’ and ‘Git Pull’


Git Pull vs Fetch

Many of us take a look at git fetch and git pull and believe them to do the exact same thing! This is just not true.

So what exactly is the difference and when should you use one over the other?

The Difference Between Pull and Fetch

Git Fetch
<p class="schema-faq-answer">
  The <code>fetch</code> command goes to the remote repository and gets the latest information about the branch, commits, references (tags) and all the file objects. This is then returned to the local git instance so that it knows what to do when you want to issue a <code>merge</code>.
</p>
Git Pull
<p class="schema-faq-answer">
  The <code>pull</code> command essentially does a <code>git fetch</code> and a <code>git merge</code> on the respective branch.
</p>

Roundup

So when would you ever use git fetch then? Well, it’s for when you want to get all the information about changes without merging those changes into your local branch and overwriting your local working copy. It is also very useful for when you want to get the meta about a tag without pulling that tag’s reference data back over your local branch.

So a git pull is similar to a git fetch followed by a git merge, but not in all circumstances. Yes, there is a complication, it is that you need to know which remote-tracking branch your local branch is tracking against. If you know this, then you can simply emulate a git pull by performing a git fetch && git merge.

Note that pull also makes sure to move your pointer (fast-forwards) your tracking branch, where-as a merge does not, it simply performs the merge action and then stops. It’s up to you to let the remote branch know that the commits have been merged to a specific commit_id.