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, runninggit pullwhile 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 pushandgit pullwork 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, runninggit checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the--trackshorthand: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:
Now, your local branch
sfwill automatically push to and pull fromorigin/serverfix.BONUS: extra
git statusinfoWith a tracking branch,
git statuswill tell you how far behind your tracking branch you are – useful to remind you that you haven’t pushed your changes yet! It looks like this:or