Start a New Branch on your Remote Git Repository

Git is a distributed version control system so it allows you to create branches locally and commit against them. It also supports a more centralized repository model. When using a centralized repository you can push changes to it so that others can pull them more easily. I have a tendency to work on multiple computers. Because of this, I like to use a centralized repository to track the branches as I work on them. That way no matter what machine I’m on, I can still get at my branches.

The Workflow

My workflow is generally something like this:

  1. Create a remote branch
  2. Create a local branch that tracks it
  3. Work, Test, Commit (repeat) - this is all local
  4. Push (pushes commits to the remote repository)

Git commands can be a bit esoteric at times and I can’t always seem to remember how to create a remote git branch and then start working on new code. There also seems to be multiple ways of doing it. I’m documenting the way that seem to work for me so that I can remember it. Maybe it will help someone else too.

Creating a Remote Branch

1. Create the remote branch
git push origin origin:refs/heads/new_feature_name

2. Make sure everything is up-to-date
git fetch origin

3. Then you can see that the branch is created.
git branch -r

This should show ‘origin/new_feature_name’

4. Start tracking the new branch
git checkout --track -b new_feature_name origin/new_feature_name

This means that when you do pulls that it will get the latest from that branch as well.

5. Make sure everything is up-to-date
git pull

Cleaning up Mistakes

If you make a mistake you can always delete the remote branch
git push origin :heads/new_feature_name
(Ok Git’ers - that has to be the least intuitive command ever.)

Use the Branch from Another Location

When you get to another computer or clone the git repository to a new computer, then you just need to start tracking the new branch again.
git branch -r to show all the remote branches
git checkout --track -b new_branch origin/new_feature_name to start tracking the new branch

Automate it A Bit

That’s a pretty easy thing to automate with a small shell script luckily


#!/bin/sh
# git-create-branch <branch_name>

if [ $# -ne 1 ]; then
         echo 1>&2 Usage: $0 branch_name
         exit 127
fi

set branch_name = $1
git push origin origin:refs/heads/${branch_name}
git fetch origin
git checkout --track -b ${branch_name} origin/${branch_name}
git pull

4 Responses to “Start a New Branch on your Remote Git Repository”

  1. Working with remote branches « Aslak Johansen and Jan Chu @ DIKU writes:

    [...] Full description at this page. [...]

  2. Collin VanDyck writes:

    Thanks very much for this; this is exactly what I was looking for.

  3. pietro writes:

    i think it is easier to first create the branch locally and then push it to the remote repo:

    git checkout -b new_feature_name
    git push origin new_feature_name

  4. Kai writes:

    Pietro,

    does this really setup tracking correctly? I had problems with that way when I modified that branch on a second computer. After I returned to the first computer where I initially setup the branch I tried to push some commited work and it told me, that the remote branch is somehow not compatible. But trying to merge the new commits into my local repo told me that there is no such remote head to sync with. Very strange. I had to rename my branch, merge the other branch into it and rebase it to commit it.

    Not sure if I did everything right because I am still pretty new to git and originally came from subversion. Git is so much nicer in many ways but the workflow is completly different if you want to use it as its full potential. So I may sometimes still to thing “intuitively wrong”.

Leave a Reply