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

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

For further help, you might want to check out:

69 thoughts on “Start a New Branch on your Remote Git Repository”

  1. @Dario, If you notice the date on mine is from 2008 and more than a year older than your post. So please don’t claim that I copied your work because you happened to write something similar more than a year after I did.

  2. @GeoffLane You may have used a Time Machine or something like that

    Seriously, both you and me have copied this commands from a dubious source… I dont known what origin means as commit specificator. is the current commit? is equivalent to origin/master?, is cleaner to put it as git push origin HEAD:refs/heads/new_feature

    Surely, git push origin origin:refs/heads/new_feature works (I’ve used it), but the meaning of origin to specify the commit is not clear

    Nice blog, just watch your sources to avoid “strange” information

  3. @Dario: Copyrigth does not apply to knowledge, which is the only thing the two articles have in common. I don’t see a copy in terms of the creative process I don’t see any copyright infringement.

  4. @GeoffLane Moral of story, break the spirit of sharing and keep all knowledge to yourself. You just cant help some people! Rest assured Geoff it helped me and countless others that didn’t comment.

  5. I am wondering what is the difference between these two lines:
    git push origin new_feature_name
    git push origin origin:refs/heads/new_feature_name

    If I have already used the first line to create a remote branch, should I delete that remote branch and create it again using the second line?

    Can I also use the –track option to checkout a branch created with the first line?

    Would be nice if you could clarify that. Also great tutorial thanks very much for sharing.

    Cheers

  6. Since I came across your article looking for something else and it is pretty well referenced despite its age, I’ll add my 2 cent of update :)

    There is an easier way to do this since version 1.7.0 of git (at least more understandable for my poor brain):
    1- Create the local branch from the current one

    git checkout -b new_feature

    2- Push that branch to the remote

    git push origin new_feature

    At this stage your local branch is set to push to the remote but pull won’t work

    3- Set the upstream branch to track

    git branch --set-upstream new_feature origin/new_feature

    Now you can both push to / pull from the remote branch

    4- If you need to delete the remote branch

    git push origin :new_feature

  7. Oliver, thanks, your updated instructions helped a lot – and Geoff, thanks for the older ones and the place for discussion.

  8. @Geoff, thanks for the post. I’ve been using this method for the past several months.
    I used your automation idea too, with a git-create-branch and git-delete-branch function.
    @Oliver, thanks for the comment I’ve adapted my code to leverage the updated method.
    I posted the code and notes here: http://pages.uoregon.edu/vid/2012/08/14/branching-with-git-%E2%80%93-follow-up-on-keeping-remote-and-local-sync%E2%80%99d/

    Here are the functions: function git-create-branch(){ # git-create-branch
    #!/bin/sh
    #from http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/comment-page-2/#comment-18065
    if [[ ! -n "$1" ]] ; then
    echo 1>&2 Usage: git-create-branch branch_name
    else
    #$1 => branch_namea
    #vars:
    GCO="git checkout -b $1";
    GPO="git push origin $1";
    GBRSUP="git branch --set-upstream $1 origin/$1";
    GBR="git branch";
    GBRR="git branch -r";
    #actions:
    echo "Adding $1"
    echo "1 - Create the local branch from the current one: '$GCO'" > /dev/tty; $GCO
    echo "2 - Push that branch to the remote: '$GPO'" > /dev/tty; $GPO
    echo "3 - Set the upstream branch to track: '$GBRSUP'" > /dev/tty; $GBRSUP
    echo "4 - If you need to delete the remote branch use: git-delete-branch $1 or git push origin :$1" > /dev/tty; echo -n;
    echo "### Local branches ($GBR) ### "; $GBR
    echo "### Remote branches ($GBRR) ### "; $GBRR
    fi
    }

    function git-delete-branch(){ # git-delete-branch
    #!/bin/sh
    #modified from script and comments at http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/comment-page-2/#comment-18065
    #if [ $# -ne 1 ]; then
    if [[ ! -n "$1" ]] ; then
    echo 1>&2 Usage: git-delete-branch branch_name
    else
    #$1 -> branch_name
    #vars:
    GCO="git checkout master";
    GPOD="git push origin :$1";
    GBRD="git branch -d $1";
    GFO="git fetch origin";
    GPL="git pull origin master"
    GBR="git branch";
    GBRR="git branch -r";
    #Verbose actions:
    echo "Removing $1"
    echo "1 - Check out master branch: '$GCO'" > /dev/tty; $GCO
    echo "2 - Delete remote branch: '$GPOD'" > /dev/tty; $GPOD
    echo "3 - Delete local branch: '$GBRD'" > /dev/tty; $GBRD
    echo "4 - fetch and pull origin: '$GFO; $GPL;'" > /dev/tty; $GFO; $GPL;
    echo "### Local branches ($GBR) ### "; $GBR
    echo "### Remote branches ($GBRR) ### "; $GBRR
    fi
    }

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>