‘git pull’ vs ‘git fetch’ — What’s the Difference (with Examples)

git pull is the same as git fetch + git merge.

When doing version control in Git, you will frequently use the commands git pull and git fetch. Understanding the difference between these two commands is important and useful for the future.

In short, git pull is just a combination of git fetch + git merge.

This is a comprehensive guide to understanding the differences between git pull and git fetch commands. You will see a concrete example with a real git repo to support understanding.

What Is ‘git fetch’?

Git fetch downloads changes from the remote repo to your local repo without merging the changes.

You should run git fetch to see what others have been working on in the remote repo. This allows you to preview how the central history has changed without having to merge the changes into your local repo.

Running git fetch doesn’t impact your local development work. So you can run git fetch any time without having to worry about merge conflicts. Git fetch is a safe command you can run before integrating the remote changes to your local repo.

What Is ‘git pull’?

The git pull command is perhaps the most popular command in Git. The git pull command is a more aggressive version of the git fetch command.

The git pull command not only downloads the remote changes but also merges them with your local repo.

As a matter of fact, git pull is a combination of git fetch + git merge.

git pull <=> git fetch + git merge

If you run git pull when you have pending changes there’s a chance it triggers a merge conflict.

‘git pull’ vs ‘git fetch’

Here is a table that summarizes the difference between git fetch and git pull commands:

git fetchgit pull
Gets information about changes in the remote repoGets information about changes in the remote repo and merges the changes to the current branch
Updates the repository data in the .git directoryDirectly updates the local directory
Preview remote changes before mergingMerge remote changes right away
No merge conflictsMerge conflicts might occur if the remote and local repos contradict
git fetch is a single commandgit pull is a combination of git fetch + git merge

Example: git pull vs git fetch

That’s enough for the theory! Let’s take a look at a concrete example of running git pull vs running git fetch.

Now, let’s make a remote change to the project repo.

After making this commit, let’s compare the local repository and the remote one:

In the local repo, git log only shows a single commit. On the other hand, the remote repo now has two commits because I just made one to it directly from the web interface.

Now there are two things you can do:

  1. Run git pull to view and merge the changes to the local repo.
  2. Run git fetch to see what has changed in the remote repo.

Let’s run git fetch and see what happens.

Take a look at the last line of the message above.

b8e3511..4a734bf  main       -> origin/main

This suggests a commit has been made to the remote repo.

To merge the commit(s) with your local repo after fetching, run git merge origin/<branch name>. In this case, let’s run:

git merge origin/main

This simply merges the fetched commits with the local repo.

Now, let’s re-run git log to see the commit history:

As you can see, now there are two separate commits in the local repository. This is because we just fetched and merged the changes from the remote repo.

This is exactly what would happen if you did git pull. But with git fetch, you were able to check if something has changed before doing the merge.

How to Find Changes After Running ‘git fetch’

As the name of the command git fetch suggests, it retrieves the changes from the remote repo.

Assuming there’s a change in the remote repo, if you run git fetch it downloads the changes and shows what commits have been made to the remote repo. This image you already saw in the previous example shows what it looks like:

git fetch logging the remote commit hashes in the console

But if you run git fetch again right after running it, you will see nothing. This is because the changes were already downloaded and there’s nothing to fetch (unless there’s a new change).

To find the changes after running ‘git fetch’, you need to run:

git log main..origin/main

Or more generally in any branch:

git log <branch-name>..<remote-origin>/<branch-name>

This shows you the available commits that are present in the remote origin but not on your local repo.

Summary

Today you learned what’s the difference between popular Git commands git pull and git fetch.

In short, you can use git fetch to see what’s changed in the remote repo. To see and merge the remote changes, run git pull. Behind the scenes, git pull = git fetch + git merge.

Thanks for reading. Happy coding!

Read Also

Git Difference Between Two Branches

Scroll to Top