Difference Between Fetch and Pull Git Commands

In this blog post, we will learn about the difference between the get Fetech and pull commands, which are commonly used when working with a centralised code repository like Azure DevOps, etc.


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. Choose what’s right for you at the moment.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode