Quick Links
Summary
The git fetch command is like a git pull command that updates your local repository with information and commits from the remote repository, without overwriting your working files.
The Gitfetchcommand lets you look before you leap. You can discover what changes have been made to a remote repository, but without overwriting your local files with the remote files.

What Is git fetch, and What Does It Do?
Thefetchcommand in Git downloads commits, files, and other information from a remote repository to your local repository, safely. Use fetch when you want to see what changes other developers have made, without being forced to accept the changes. Your local files remain untouched. Why is that important?
You have to keep your wits about you when you’re working as part of a development team. The remote or central repository will contain changes and new branches that other developers have created and pushed to the remote repository.

It’s entirely possible that someone has updated the remote copy of files that you’ve modified locally on your computer. If you casually perform agit pullto update your local repository, you’re likely find to yourself handling merges for changes you might not even want.
Once you’ve downloaded the information, you may examine it and see what the changes are. This lets you make an informed choice about what you want to merge right now, if anything, and what you want to defer until later.

Git fetch vs. pull
Thegit fetchcommand behaves like thegit pullcommand but without the step that overwrites your local files. Your local repository is updated and synchronized, but the changes are not written to your local repository’s working state, so your files remain untouched.
Or, to put it another way, thegit pullcommand is like agit fetchfollowed immediately by agit merge.

Sync Your Local and Remote Repositories With git fetch
To fetch all the updated metadata and commits from a remote repository to your local repository, use thegit fetchcommand with the name or URL of the remote repository. By default, the first remote repository is called “origin.”
you’re able to omit the word “origin” if you’re working with a single remote repository.

That retrieves any updates from the “origin” repository, but it doesn’t merge the changes into the working files. We can see there is a new branch, called “new-branch”, that has been retrieved for us.
Once you’ve used thefetchcommand, you can see see the full list of branches on the remote, by using-r(remote) option with the branch command.

This lists all the branches that the remote knows about, which after thefetch, are also in your local repository.
See All Fetched Tags
Similarly, you can use thetagoption (note, it’s “tag” without an “s”) to see the list of tags.
Doing a Dry Run First
Although agit fetchdoesn’t merge the changes into your working files, it does still update your local repository. If you want to see what change thefetchcommand will perform, without actually making them, use the–dry-runoption.
How to Fetch a Single Branch
Fetching information about a single branch is easy. Addthe name of the branchto the command line to tellfetchyou only need to know about that one branch.
Here, we’re tellingfetchto retrieve the branch “mary-feature” from the remote repository “origin.”
Now that the details and contents of the remote branch are in your local repository, you’re able to use thegit checkoutcommand to create a new branch andcheckout the remote branch. This won’t over-write any existing files if it’s the first time you’ve used this branch.
Fetch All Branches
If you’reusing multiple remotes, you can save time by pulling all changes from all branches back to your local repository by using the–alloption.
Comparing Local and Remote Branches
To see how the files in a remote branch differ from your local copies, usegit fetch, then use thegit logcommand.
Note that the local and remote branches have two periods “..” separating them. The–onelineoption shows thecommitidentifier and the commit message.
The one-line display is useful if a branch contains a great many changes. To see a little more information, omit the–onelineoption.
This shows us the time and date of each commit, together with the commit message and the contact details of the change author.
Synchronizing a Local Branch with a Remote Branch
If you’ve decided you want to go ahead and merge the changes from the remote branch to your local working files, you can use these commands.
We’ll checkout the branch to make sure we’re working it is our current, working branch..
The branch is checked out for us, and we’re told that it is behind the remote version. We can usegit pullto update it, thengit statusto check our status.
If we’ve made some changes to our local files, Git informs us when we checkout the branch that we’ll need to perform agit pullto start a merge.
A simplegit pullstarts the process of retrieving the files and merging, or we can dive right in and usegit mergeitself. We’ll start by making sure we’re working with the correct branch
We’ll tellgitto merge our current branch with the branch in the remote repository, there are six different commits that need to be resolved.
We need to tell Git which remote branch we want to merge.
Aneditoropens to allow us to provide a commit message. We can accept the suggested message, or add our own. The editor is yourdefault editorunless Git has been configured to use a different editor.
Save your changes when you are ready to proceed. The merge carries on automatically when the editor is closed.
Our merge was successful because there were no conflicts.
Conflicts arise when the same lines of code are changed by two or more developers. If that is the case,Git marks up the conflicts in the affected file. You’ll need to review them in turn and choose which change to keep.
The Half-Trained Puppy
Just like a puppy in training,fetchwill retrieve what you ask for, but it won’t drop it. If you actually want what the puppy has shown you, you’ll need topull.