Git — pull vs. fetch

Ever wondered about the difference between fetch and pull? This short article looks at the similarities, differences, and when to use one over the other to improve your workflow.

What is fetch?

Fetch (it’ll come as no surprise) ‘fetches’ new data from the remote repo. That’s all, it doesn’t modify your working directories in any way, just makes your local repo aware of the changes that have occured on remote.

What is pull?

Like fetch, ‘pull’ is a mechanism for getting new data from remote. The difference is, once the data arrives it automatically merges the changes into the current working branch. Pull is actually performing a fetch on your behalf in order to get the new data.

Pull is destructive and you can find yourself knee deep in merge conflicts if you aren’t prepared for the changes.

Safely integrating remote changes.

Pull takes modifying your local files completely out of your hands. Sometimes this is desired when you have a good idea of what has been going on remote and your working directory is clean. However when part of an active team all contributing through-out the day, the code you’re working on can radically change between pulls.

A safer way of integrating remote changes is to first fetch from remote. Those changes can then be inspected to find potential conflicts before they arise. At that point, if all is well, you can then merge.

Here’s an example of a flow you might use:

 # Our current working branch is master.

 # Get all remote changes
 git fetch

 # Inspect our local changes with those that have occured on remote
 git diff master origin/master

 # Merge the remote changes into our working branch.
 git merge origin/master

comments powered by Disqus