A tracking branch is a branch that is linked to a remote branch.
A local branch can track a remote branch using git branch
command with long option --set-upstream-to=
or short option -u
followed by a branch name. If no branch name is specified, then it defaults to the current branch. For example,
git branch -u origin/issue-1
or longer option:
git branch --set-upstream-to=origin/issue-1
When you clone a repository, it generally automatically creates a main
branch that tracks origin/main
. That's why git push
and git pull
work out of the box with no other arguments.
You can Check out a local branch from a remote branch by creating what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git push
, Git automatically knows which server and branch to push to. Also, running git pull
while on one of these branches fetches all the remote references and then automatically merges in the corresponding remote branch.
git checkout --track origin/serverfix
That command creates a local serverfix
branch with the same content of origin/serverfix
, and makes it a tracking branch of origin/serverfix
.
To set up a local branch with a different name than the remote branch:
git checkout -b sf --track origin/serverfix
That command creates a local sf
branch with the same content of origin/serverfix
, and makes it a tracking branch of origin/serverfix
.