Difference Between Git Pull and Fetch Commands

In this blog post, we will learn about the difference between the get Fetch and pull commands, which are commonly used when working with a centralised code repository like Azure DevOps, etc. These fetch and pull commands we use regularly while working with the centralised code repository, but still sometimes we confuse ourselves, like when we see the pull and push written on the doors. So let's learn about these commands and know when to use them effectively based on the use-case scenario.

Consider the scenario. Our code repository is hosted on Azure DevOps, and we are using code editors like Visual Studio or Visual Studio code. We need to use the following commands, either through the command line or using the visual options available in our code editor tools.

Git Fetch

The "git fetch" command is used to retrieve changes from a remote repository to your local repository. It doesn't automatically merge or apply these changes to your local branch. It only updates your local references to the remote branches.

Scenario

when you want to see what changes are available in the remote repository without merging them into your working branch immediately. It's helpful for reviewing changes before deciding to incorporate them into your codebase.

In short, use "git fetch" when you want to see the new changes in the remote repo but don’t want to change your local repo yet.

Example

#Fetch changes from the remote repository (origin in this case)
git fetch origin

#List branches, including remote branches, to see what's available
git branch -a

After using fetch, your local repo stays the same until you decide to add the new changes.

Git Pull

The "git pull" command is used to fetch changes from a remote repository and automatically merge them into your current local branch. It is essentially a combination of "git fetch" and "git merge."

Scenario

When you want to quickly update your local branch with the latest changes from the remote branch, you are fine to merge them into your working branch after verifying the changes and knowing any implications.

Example

#Pull changes from the remote repository (origin) and merge them into the current branch
git pull origin dev

In this example, "dev" is the branch you want to pull changes from. The "git pull" command will fetch changes from the remote "dev" branch and merge them into your current local branch.

Summary

In summary, use "git fetch" to just see the new changes from the remote repo, and use "git pull" when you are ready to add those changes to your local repo quickly. I hope this blog post is useful to help you decide what's the right scenario for you to choose between git fetch and pull while working with the centralised code repository.

Following are my other articles, if you are interested in reading about them.