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

1
git push origin origin:refs/heads/new_feature_name

2. Make sure everything is up-to-date

1
git fetch origin

3. Then you can see that the branch is created.

1
git branch -r

This should show ‘origin/new_feature_name’

4. Start tracking the new branch

1
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

1
git pull

Cleaning up Mistakes

If you make a mistake you can always delete the remote branch

1
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.

1
git branch -r

to show all the remote branches

1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/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

For further help, you might want to check out:

About Geoff Lane

I’m Geoff Lane and I write Zorched.net as I figure things out about software development in the hopes that it can help other people facing similar situations. Also as a thanks to the larger web community for all of the information and knowledge that they have shared. I’ve been a professional software developer since 1999 working with a variety of different technologies. I’ve worked for startups in the Silicon Valley and Chicago, IL and now work as a consultant building custom applications for clients.
This entry was posted in Automation, Code and tagged , , . Bookmark the permalink.

69 Responses to Start a New Branch on your Remote Git Repository

  1. Dario says:

    This is a enhanced copy of an old post on my blog (spanish)

    http://nilclass.blogspot.com/2009/08/branches-remotos-en-git.html

    Even the push command has copied wrong

    Is:
    git push origin HEAD:refs/heads/new_feature_branch

    Not:
    git push origin origin:refs/heads/new_feature_branch

    However, the more practical method to create a branch from current commit is:

    git checkout -b new_feature_branch
    git push -u origin new_feature_branch

  2. Geoff Lane says:

    @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.

  3. Pingback: Git: Links, News, Resources (1) « Angel “Java” Lopez on Blog

  4. Peter von Kaehne says:

    Many thanks! This was a useful post.

  5. Dario says:

    @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

  6. Pingback: » Push branch to server / remote location Emil's tech stuff

  7. Paul Moore says:

    Thanks Geoff – that really helped me.

  8. @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.

  9. Peter says:

    @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.

  10. Pingback: How to add and delete remote branches in git

  11. Pingback: Git branches | WP Cookbook

  12. Lanoxx says:

    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

  13. Great tip, helped a lot.

  14. Pingback: Remote Git branch | To the Great National Parks!

  15. Olivier Clavel says:

    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

    1
    git checkout -b new_feature

    2- Push that branch to the remote

    1
    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

    1
    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

    1
    git push origin :new_feature
  16. Geoff Lane says:

    @Olivier, thanks for posting a new method to do this. That’s the main problem with these blog posts is that they get stale. :)

  17. Paul Jacobs says:

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

  18. vid says:

    @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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    function git-create-branch(){ # git-create-branch <branch_name>
      #!/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
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    function git-delete-branch(){ # git-delete-branch <branch_name>
      #!/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
    }
  19. Pingback: Learning my ways with git | Eric's blog

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>