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.
This tutorial will discuss, with an example, the basics of the git fetch command, how it works, and when you will want to use it. 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 which keeps track of the changes the developer has 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 from the main code.
Git Fetch vs. Git Pull
If you want to retrieve the changes made to a remote repository, there are two ways to do so: using git pull and git fetch.
Both of these commands download the contents of a repository from a remote repo to your local machine. The git fetch command allows you to download a repository without forcing changes to your local copy of a repository.
The git pull command, however, will change the code you have stored on your local machine, which means that it 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, which is used to make 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.
So, if you want to retrieve code without changing your current working copy of a repo, you should use the git fetch command. Let’s discuss in more depth how this command works to illustrate how you can use it in your Git repositories.
How the git fetch Command Works
By default, the git fetch command fetches all branches and/or tags (which are collectively called “refs”), and 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. So, when we run git fetch, we can store a copy of both the current state of our repository on our local machine and the copy of the code we retrieved from the remote repository.
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:
This command, in an example repository, returns:
* master update-index
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:
git branch -r
The -r flag stands for “remote”, which instructs the branch command to return a list of remote branches. The command returns:
origin/master origin/update-index remote/master
This list shows all the local branches we have (which have the prefix “origin/”), as well as 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
This command allows us to view the code on the remote/master branch.
Now, if we want to create a new local branch with this code, we can use the following command while viewing the remote/master branch:
git checkout -b remote_master_local
The above command creates a branch on our local machine called “remote_master_local”, which contains all the code that exists in the remote/master branch. So, 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 the git checkout command works, read our git checkout tutorial.
The git fetch command is used to download the contents from a remote repository. Developers use the git fetch command in addition to the git checkout command to view the code retrieved using the git fetch command and work with it on their local machine.
The git fetch command is similar to git pull, but git pull directly changes your local working copy of a repository. As a result, git pull is not always useful.
This tutorial discussed, with examples, the basics of retrieving code, how git fetch compares to git pull, and how to use the git fetch command. Now you’re ready to start fetching code using the git fetch command like a pro!