Post navigation

Git

git fetch vs pull

Should I use git fetch or git pull? What does git fetch do exactly?! In this post I’ll clarify the difference between the two git commands – and to answer the git fetch vs pull question.

In summary it’s a simple difference – git fetch ‘fetches’ all of the information about your git repo, so you can see what branches have changes – but you don’t actually pull down any of the file changes. Git pull on the other hand, gets the information AND all of the changed files. Read on for a little more detail about each command.

What is git fetch?

Let’s say you’re working as part of development team and you are all working in the same repo. If 5 of your colleagues had pushed new code into the repo, on Monday morning you might want to know exactly what changes are waiting for you to pull down. That’s what you can use git fetch for – it’s all of the changed file information from the remote branch. So why would you use it? It’s useful if you are expecting to have some conflicts from changes that have been made in the remote branch. Maybe you know a colleague has been working in the same part of the code as you. Once you are in a merge conflict situation you have to resolve it before you can continue coding, so getting some information on which files are involved in the conflict can be really useful to avoid that scenario – and that’s where git fetch is really useful. It can also be useful if you are working on a restricted internet connection, and if some of the files that may need to be pulled are very big.

What is git pull?

git pull = git fetch + git merge

So git pull ‘fetches’ all of the changed file information and then merges the changed files into your local branch. It updates your current local branch with any new files that have been added to the remote branch since you last pulled. So you need to make sure you are on the correct branch before you run git pull. If there are any conflicts caused by the new files being merged into your local branch will have a merge conflict to resolve.

In conclusion

When considering the git fetch vs git pull question, most of the time I just use git pull, especially if I’m working on a solo project or on a very small team where the files are small. But it’s certainly useful to know what git fetch does and when to use it.