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?
fetch 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
pull command essentially does a
git fetch and a
git merge on the respective branch.
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.
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.
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.