2 Mar 2012

Git – tracking branches

In Git, if you find yourself constantly typing git push origin branchname to push your local commits to the remote branch, here’s a tip: make your local branch automatically track your remote branch.

When creating a new remote branch, here’s how you can make your local branch track its remote branch:

# creates a new local branch
git branch foobranch  
 
# creates a new remote branch, and makes local branch track that (-u)
git push -u origin foobranch

If you want to set-up tracking for an existing branch, you can do so by:

# set-up tracking for an existing branch
git branch --set-upstream foobranch origin/foobranch

If you are checking out an already existing remote branch, you can set-up tracking in a single command:

git branch --track foobranch origin/foobranch
 
# or, alternatively
git checkout --track origin/foobranch
 
# or
git checkout -b foobranch origin/foobranch

Setting up tracking offers two advantages. Firstly, it reduces the number of characters you have to type to push your changes to a remote branch. More importantly, it prevents you from accidentally typing git push which will end up pushing out the local commits in all your other branches as well (which you might not be ready to push yet!).

Of course, you can also set-up tracking by directly modifying your .git/config file!

Tagged with:

4 people have responded to this post.

Leave a Reply