Can someone explain a "tracking branch" as it applies to git?
Here's the definition from git-scm.com:
A 'tracking branch' in Git is a local
branch that is connected to a remote
branch. When you push and pull on that
branch, it automatically pushes and
pulls to the remote branch that it is
connected with.
Use this if you always pull from the
same upstream branch into the new
branch, and if you don't want to use
"git pull" explicitly.
Unfortunately, being new to git and coming from SVN, that definition makes absolutely no sense to me.
I'm reading through "The Pragmatic Guide to Git" (great book, by the way), and they seem to suggest that tracking branches are a good thing and that after creating your first remote (origin, in this case), you should set up your master branch to be a tracking branch, but it unfortunately doesn't cover why a tracking branch is a good thing or what benefits you get by setting up your master branch to be a tracking branch of your origin repository.
Can someone please enlighten me (in English)?
The ProGit book has a very good explanation:
Tracking Branches
Checking out a local branch from a remote branch automatically creates 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.
When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don’t track branches on origin and don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]
. If you have Git version 1.6.2 or later, you can also use the --track
shorthand:
$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"
To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:
$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "sf"
Now, your local branch sf
will automatically push to and pull from origin/serverfix
.
The Progit book mentions:
Tracking branches are local branches that have a direct relationship to a remote branch
Not exactly. the SO question "Having a hard time understanding git-fetch
" includes:
There's no such concept of local tracking branches, only remote tracking branches.
So origin/master
is a remote tracking branch for master
in the origin
repo.
But actually, once you establish an upstream branch relationship between:
- a local branch like
master
- and a remote tracking branch like
origin/master
Then you can consider master
as a local tracking branch: it tracks the remote tracking branch origin/master
which, in turns track the master branch of the upstream repo origin
.
This was how I added a tracking branch so I can pull from it into my new branch:
git branch --set-upstream-to origin/Development new-branch