What is the Difference Between Git fetch and Git pull?

The two most frequently commands used to update a local repository against a remote repository are git fetch and git pull. But what do they do? And what are the differences between them?

Let's find out!

Git fetch

The git fetch command is used to keep a local repository in sync with the remote repository.

When you run git fetch, it will update the local repository so that it is in sync with remote repository. But, it will not modify anything in the working directory, neither it will modify any of your local branches.

Since git fetch does not modify contents of your working directory, it is useful to run git fetch once in a while to make sure if there are any changes in the remote repository.

It helps you learn about the changes that have occurred in the remote repository since you last synchronized. And, since this command's execution does not modify local branches, it is considered a 'safe operation'.

Git pull

The command git pull does two things.

When you run git pull, it will run git fetch to make sure that the local repository is aware about the developments that have occurred in the remote repository.

Now that the local repository is in sync with remote repository, another command gets executed. That command is git merge FETCH_HEAD.

The git merge FETCH_HEAD command will merge changes from the remote HEAD branch into the currently active local branch.

This essentially means, that "fetch changes from remote repository and the default remote branch into the branch that I am working on".

If you do not mange local branches carefully, running git pull can be end up being problematic. Use this command only if you are 110% sure, or that you are able to resolve merge conflicts.

Conclusion

The commands git fetch and git pull are often used interchangeably.

The difference between them is that git fetch will only cache the changes that have occurred in remote repository, it will not make any changes to your local repository.

Whereas, git pull will not only do what git fetch does, but also merge those changes from remote repository into local repository.

To learn more about Git, you can head over to our main site.