Friday, January 21, 2011

start tracking existing branches with git

Git has the concept of "tracking branches": a local branch can be linked to a remote branch so pull and push commands have a default destination on the remote repository.

As an added benefit, you will get an informative message on top of "git status" output telling you which one of the two ends has new commits, like this:
$ git status
# On branch master
# Your branch is behind 'origin/master' by 11 commits, and can be fast-forwarded
Now, this works by default with the master branch but if you created a local branch like:
$ git checkout -b new_branch
Switched to a new branch 'new_branch'
then decided to push it to origin (or any other remote), tracking will not be active unless you use the command:
$ git push --set-upstream origin new_branch
Counting objects: 40, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 3.25 KiB, done.
Total 25 (delta 19), reused 0 (delta 0)
   dcdb736..643a3f2  new_branch -> new_branch
Branch new_branch set up to track remote branch new_branch from origin
If you forget to use the --set-upstream option during push you can also do it later (from version 1.7) with:
$ git --set-upstream origin new_branch