Signup/Sign In

Git Pull vs Git Fetch

Git Fetch and Git Pull commands are both used to download content from a remote repository to our local repository. Git Pull integrates the working of two different commands and one of those commands is Git Fetch. In this article, we'll see some key differences between them.

Pull vs Fetch

Pull Fetch
Git Pull downloads the content from the remote repository and also merges the new commits into our working branch. Git Fetch is a command which just copies or downloads content from the remote repository into the local repository.
After pulling we will have a branch that has the local commits as well as the new commits that were added to the remote repository. It updates the remote-tracking branch to make sure that we can see the changes but still keep our local working branch intact.
Git Pull combines the working of two different Git Commands - Git Fetch and Git Merge. So internally Git Pull first calls the Git Fetch command to download the content from remote repositories to the local repository and then calls the Git Merge command to add these changes to the local working branch. Git Fetch can be considered as a harmless option as our working branch is not altered and our uncommitted work is secure.

Illustration to show the difference between Git Fetch, Git Pull and Git Merge.

Let's try to understand this difference better and the use of remote-tracking branches with the help of the following illustration.

  1. Suppose we cloned a remote repository having three commits. We will have the remote-tracking branch reference origin/master and the local master branch reference pointing at commit point C.After cloning, both local master ref and remote tracking branch origin/master ref point to the same commit.
  2. Next, we add one commit to our local master branch and some other developer adds two more commits to the remote repository.After adding two commits to the remote repository and one commit to the local repository

  3. Now if we run the Git Fetch command the two new commits of the remote repository will be added to the remote-tracking branch.After running the git fetch command the two new commits of the remote repository are added to the remote tracking branch

  4. But instead, if we run the Git Pull command the two commits will be fetched just like in the above step but will also be added to the local master branch in the form of a new merge commit point G. Thus our local working branch will be altered.Git Pull command first fetches the two new commits and also merges them to the master branch by creating a new commit point.

Which one is Better?

No one command can be considered superior to the other one. Both Git Fetch and Git Pull have separate jobs and are used according to the needs of the developer. Consider the following scenarios to better understand when to use which command.

  • Say for example that a developer is working on a project in a team and has unsaved changes or pending commits in the working directory but still wants to check the status of the remote repository. The developer can use the Git Fetch command to look at the new commits that were added to the remote repository. Git Fetch is often considered as the safe command as it does not alter our working directory.
  • Instead, if the developer has finished his/her part of the job and is ready to combine it with the work of other developers working on the same project then he/she may use the Git Fetch command.

Summary

Git Pull and Git Fetch are two very important commands which we will often use to interact with our remote repository. We learned that when we just want to take a look at the changes in the remote repository without affecting our local work we can use the Git Fetch command but instead if we want to merge the new commits of the remote repository with our local repository then we may use the Git Pull command. Both of these commands have their own use cases and benefits but we still consider the Git Fetch command as a safer option.