The Branch Exists Only On The Remote
Learn how to deal with a branch that exists only on the remote repository.
We'll cover the following
Branch on remote only#
It is common to have a branch that exists on a remote repository but not in your local repository. Maybe someone else pushed a branch up or has made a pull request from a branch in that remote repository.
Example#
Type the following out to simulate that state of affairs:
1 mkdir git_origin
2 cd git_origin
3 git init
4 echo 'first commit' > file1
5 git add file1
6 git commit -am file1
7 cd ..
8 git clone git_origin git_clone
9 cd git_origin
10 git checkout -b abranch
11 echo 'origin abranch commit' >> file1
12 git commit -am 'cloned abranch commit'
13 git branch -a
14 cd ../git_clone
15 git branch -a
16 git remote -v
You will observe that the cloned repository does not have knowledge of the abranch
branch on the origin
repository even though the origin
is known to the cloned repository. There isn’t any magic about the tracking of a remote repository. You have to trigger your repository to read the remote’s state.
Fetch branch to your repository#
To get the branch into your repository, you will need to fetch it.
17 git fetch origin
Note that you didn’t need to specify a branch to get from the origin. By default, Git will get all branches that may be of interest.
18 git branch -a
Now your cloned repository has knowledge that a branch called abranch
exists on the origin remote. But there isn’t a branch in your local repository:
19 git branch
Tracking remote branch#
If you check out an abranch
branch in your local repository, Git is smart enough to match the name and use this branch to track the remote branch from the origin:
20 git checkout abranch
21 git branch -a -vv
Pay close attention to branch tracking, as it can be very confusing to Git newcomers!
Now if you git push
any changes on this branch, Git will attempt to push those changes to the tracked branch, i.e, the abranch
branch on the remote repository.