Skip to main content

Explore your training options in 10 minutes

Git Fetch: A Step-By-Step Guide

James Gallagher - December 29, 2020

The git fetch command downloads all branches, tags, and data from a project to the local machine. Existing local code is not overwritten. Fetch is commonly used with the git reset command to bring a local repository up to date with a remote repository.

Fetching data such as files and commits from a remote repository is an important part of working with the Git version control system.

The fetching process allows you to retrieve commits, files, references , and other data from a remote repository and save it to your local machine. This allows you to download a record of how a remote repository has changed over time which you can view in your local repo.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

This tutorial will discuss, with an example, the basics of the git fetch command. By the end of reading this tutorial, you’ll be an expert at fetching code using the git fetch command.

Retrieving Code in Git

In most cases, when you’re working with Git you will have a local copy of a repository and a remote copy of a repository. The local copy of the repository is stored on your computer, while the remote copy is stored on another server.

The remote repository will contain the master code for your project. When someone makes a change to a repository that they want everyone to see, they will push it to the remote repository. The remote repository keeps track of the changes all developers have made to the code in the repo.

However, when someone pushes a change to a remote repository, those changes will not be reflected in your local copy of the code. This is because Git allows everyone to keep their own local copies of code, which they can change independently of the main code.

How the git fetch Command Works

By default, the git fetch command fetches all branches and/or tags (which are collectively called “ refs ”). The fetch command also retrieves any other data used to compile the histories of those changes. The syntax for this command is as follows:

git fetch origin

When we run this command, a copy of our remote repository (located at “origin”) is downloaded and saved to our local machine. However, the local copy of our code has not yet been changed.

This is the case because, unlike git pull, the git fetch command does not perform a merge operation on our code.

We can see how this works by executing the git branch command. Suppose we have a demo repository with two branches. To retrieve a list of these branches, we can run the following command:

git branch

This command, in an example repository, returns:

* master

As you can see, our repository has two branches. If you’re interested in learning more about the git branch command, read our guide to the git branch command .

Now, because we run git fetch, we also have a copy of the remote branches of our repository. We can view these using the following command:

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

git branch -r

The -r flag stands for “remote”, which instructs the branch command to return a list of remote branches. The command returns:


This list shows all the local branches we have (which have the prefix “origin/”). In addition, we can see the remote branches on our remote repository called “remote”.

This demonstrates that Git can store both your local copy of a repository as well as the remote branches you have retrieved.

Git Fetch Example

Let’s walk through an example to illustrate how the git fetch command works.

Suppose we have set up a remote repository called “remote” which stores the main code for our project.

Our coworker has just notified us that they have made changes to the remote repository, which we need to review. To retrieve those changes and save them to our local machine, we can use the following command:

git fetch remote master

This command allows us to retrieve all the code on the “master” branch in our “remote” repo.

After running this command, we now have all the code from the “master” branch saved to our local machine. But how do we actually start working with the code we have fetched? That’s where the git checkout command comes in.

The following command allows us to view the code we retrieved:

git checkout remote/master

We can use the following command to create a local branch that contains all the code on our remote master branch:

git checkout -b remote_master_local

The above command creates a branch on our local machine called “remote_master_local”. Now that we’ve created this branch, we can navigate to it and start working with the code we fetched.

If you’re interested in learning more about how to navigate to different branches, read our git checkout tutorial.

Git: Fetch a Remote Branch

You can fetch a remote branch using the git fetch command. This lets you retrieve the metadata associated with a particular branch instead of every branch.

To fetch a remote branch, you must specify the name of the remote branch that you want to fetch. You must also state the name of the local branch that is associated with the remote branch. The syntax for fetching a remote branch is as follows:

git fetch repo <remote-branch>:<local-branch>

Consider the following example:

git fetch origin dev:dev

The first command retrieves the “dev” branch from our remote repository. Only the metadata for the “dev” branch is retrieved. This branch is associated with the “dev” branch on our local machine.

In most cases, the name of your remote and local branches will be the same. This is the default behavior when Git clones a repository.

Git Fetch vs. Git Pull

Both git fetch and git pull download the contents of a repository from a remote repo to your local machine. The git fetch command only downloads the metadata associated with a project. The git pull command downloads all files and saves them to your local copy of a repository.

git pull will change the code you have stored on your local machine. The pull command may overwrite changes you have made to the local copy of a repo.

This is because, once git pull is run, a merge operation is initiated. This merges operation makes your local working copy identical to the code git pull has retrieved. You can learn more about how git pull works in our guide to the git pull command .

If you want to retrieve code without changing your current working copy of a repo, you should use the git fetch command. If you want to download all the changes made to a remote repository, use the git pull command.


The git fetch command is used to download the contents from a remote repository. Developers use the git fetch command and the git checkout command to work with code on a different branch.

The git fetch command is similar to git pull. git pull directly changes your local working copy of a repository. The git fetch command, on the other hand, only retrieves the metadata associated with a remote repository.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse,, Afrotech, and others.

Skip to main content