How to create a brand new git tracking branch (from scratch)?

There’s tons of advice on the web on how to setup a new local branch that tracks and existing remote branch. But what I was looking to do is create a brand new branch (i.e. not yet present neither on hte server nor on your workstation) and have it be a tracking branch.

After lots of goolging, reading, and trial and error in new dummy git repositories I figured out 2 ways of doing it. Here they are, the first one being (IMHO) a more elegant solution:

Solution #1
Create a brand new git branch on the server, then start tracking it:

Let’s assume you want to create a remote branch called f1_bugfixes off of an existing remote branch f1. You can then fetch it (since it will be present on the server) and track it with a local tracking branch. Here’s how to do this:

git push origin f1:f1_bugfixes

After executing the above git command the f1_bugfixes branch (or remotes/origin/f1_bugfixes, to be exat) will be present on the remote server, branched off of the f1 (remote) branch.

To track it:

git branch --track f1_bugfixes origin/f1_bugfixes

Solution #2
Create a local branch, do your work, push, then “–set-upstream” to start tracking it:


git checkout f1 # point your repository to where you want to branch off of
git checkout -b f1_bugfixes # create the new local branch
  (do your work, add, commit)
git push origin f1_bugfixes # push your branch and the changes on it to the server
git branch --set-upstream f1_bugfixes origin/f1_bugfixes

As you can see approach #2 is lengthier and hence more error-prone.

Another shortcoming of it is that you can not create the branch without pushing some “real” changes to the server. This means that this approach will not work for a project manager who wants to setup a branch point for his/her colleagues. Whereas with the first one you simply stop after step 1, that is after git push origin f1:f1_bugfixes and, voila – your branch, brand new and EMPTY(!) is created on the server!!

NOTE: As explained in the very beginning – the idea here is to create a brand new branch on the server (and then start tracking it). For all the other folks who want to use your branch the command of course will be (once you create the branch using either approach 1 or 2):

git branch --track f1_bugfixes origin/f1_bugfixes

Leave a Reply

Your email address will not be published. Required fields are marked *