Get Remote Repository Information to Local Repository – Git Fetch
Fetch is used when you want to get the latest status of the Remote Repository. The command used to execute Fetch is git fetch
. The command doesn’t require a branch name. You’ll get the information for all branches under the Remote Repository. To check the Remote Repository branch status, run the git branch -a
command. The command with the -a
option shows the status of the remote-tracking branches, which track the upstream branches from the local computer.
There are two major use cases for git fetch
. The first use case is more important than the second one as the second one can be managed by using the git pull
command.
1. Bringing a new branch from the Remote Repository
As explained on the previous page, the git pull
command doesn't work for a new branch. The git fetch
command is useful when a new branch is created in a Remote Repository and you need to bring it to your Local Repository. The typical command execution process is as follows.
First, run the fetch command to get the Remote Repository data. Then, check the branch name you want to get.
git fetch origin
git branch -a
You'll see several branch names with different colors like the example below.
* [Current Branch Name]
[Local Branch Name]
remotes/origin/[Branch Name]
remotes/origin/HEAD -> origin/master
remotes/origin/master
- Green The current local branch
- White Local branches
- Red Remote branch information. remote/origin/HEAD defines the branch that will be checked out when you clone the repository
Finally, switch to the new branch from the remote repository by running the git checkout
or git switch
command.
git checkout [Branch Name in the Remote Repository]
2. Updating an existing branch with the latest status of the Remote Repository
You can use the git pull
command to update an existing branch, however, if you are not sure about the branch status in the Remote Repository, you can run the git fetch
command first followed by the git merge
command as shown below.
First, run the fetch command to get the Remote Repository data.
git fetch origin
Then, run the merge command from the branch you want to update. For the argument, add origin/
before the branch name you want to update.
git merge origin/[Local Repository name]
Note: remote-tracking branch and upstream branch
To have a better understanding of the role of Fetch, understanding the two concepts may be helpful: a remote-tracking branch and an upstream branch. If you are new to Git, you can skip this section. You can come back here when you have built a basic understanding of Git.
- Remote-tracking branch: the branch tracks a Remote Repository from the Local Repository (e.g., origin/master, origin/Branch_A)
- Upstream branch: the Remote Repository is tracked by a remote-tracking branch
The following explanation provided in the official Git documentation may give you a better idea about the remote-tracking branch.
"Remote-tracking branches are references to the state of remote branches. They’re local references that you can’t move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the Remote Repository. Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.
Remote-tracking branch names take the form <remote>/<branch>. For instance, if you wanted to see what the master branch on your origin remote looked like as of the last time you communicated with it, you would check the origin/master branch. If you were working on an issue with a partner and they pushed up an iss53 branch, you might have your own local iss53 branch, but the branch on the server would be represented by the remote-tracking branch origin/iss53."
For a better understanding, please go through the following practice section.
Practice
Objective:
Check how the git fetch command works
1. Check the limitation of the pull command
In this part, we'll explain the steps to do this from the point of view of Developer B.
Subscribe now for
uninterrupted access.