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

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.

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

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

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

  3. pietro says:

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

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

  5. (take from http://cheat.errtheblog.com/s/git):

    git config branch.autosetupmerge true
    Tells git-branch and git-checkout to setup new branches so that git-pull(1)
    will appropriately merge from that remote branch. Recommended. Without this,
    you will have to add –track to your branch command or manually merge remote
    tracking branches with “fetch” and then “merge”.

  6. Pingback: Vertical Visions :: A technical Blog about Scripting, Monitoring and so on...

  7. Pavan Mishra says:

    Quite helpful and concise. Thanks.

  8. If someone happens to have a problem with pushing updates, at this command:

    git checkout –track -b new_branch origin/new_feature_name

    Instead of “new_branch” use the same name of the “new_feature_name”. If you don’t git won’t be able to figure out where to push your changes.

  9. You can also delete a remote branch with:

    git branch -r -d origin/new_feature_name

  10. Pingback: Tims Blog » Blog Archive » Time to use vacation

  11. Michael Johnson says:

    @kai & @pietro (3 & 4): (I know I’m late, but… just saw this)

    No, this doesn’t automatically add tracking (even with Nikos’ hint). Just tried it. What you *can* do, is to edit the .git/config file and add the following (after doing the push):

    [branch "new_feature"]
    remote = origin
    merge = refs/heads/new_feature

    You can also do the same using git config:

    git config branch.new_feature.remote origin
    git config branch.new_feature.merge refs/heads/new_feature

    I tend to do the edit, though.

  12. Michael Johnson says:

    I’ve just added a simple alias to my ~/.gitconfig file that lets me “publish” an existing branch like:

    git publish branchname

    So I can create my branch and publish it later. The alias is:

    [alias]
    publish = !sh -c ‘git push origin \”$0\” && git config \”branch.$0.remote\” origin && \
    git config \”branch.$0.merge\” \”refs/heads/$0\”‘

    I’d like to modify it so it will publish the current branch, but for now this will work, I believe.

  13. Pingback: Creating remote branches from local ones in Git | Question Defense

  14. Pingback: Working with remote GIT branches « Aslak Johansen and Jan Chu @ DIKU

  15. kbro says:

    In the command “git push origin origin:refs/heads/new_feature_name” to create the remote branch, is the second “origin” correct? This is the <src> in the <src&gt:<dst&gt refspec, so I’d expect it to be a branch name such as “master” or a revision name such as “HEAD”. This would then be the point at which the new branch came off its parent, which is what would make sense to me.

    I stared at http://www.kernel.org/pub/software/scm/git/docs/git-push.html for a long time to understand what you’re doing here.

    Other than that, it’s a great post and answered the question of how to create a branch in a remote respoitory and then use it, tracked, locally.

  16. Pingback: djwonk › Tracking Remote Git Branches

  17. David James says:

    Thanks so much for this post and the comments. I read it and others and tried to synthesize what people have already said:
    http://djwonk.com/blog/2009/04/18/tracking-remote-git-branches/

    My goal was to zero in on the simplest way of starting with a local branch, pushing to a remote branch, and getting automatic tracking to work.

  18. Pingback: When in doubt… › Creating (and removing) remote Git branches

  19. Thanks for this bro.

  20. Justin says:

    Bash Friendly Script:

    #!/bin/sh
    # git-create-branch <branch_name>
     
    if [ $# -ne 1 ]; then
             echo 1>&2 Usage: $0 branch_name
             exit 127
    fi
     
    branch_name=$1
    git push origin master:refs/heads/$branch_name
    echo "git push origin master:refs/heads/$branch_name"
    git fetch origin
    git checkout --track -b $branch_name origin/$branch_name
    git pull
  21. Vivek says:

    @Mauricio. Thanks for the advice. I got it to work now.

  22. skiedr says:

    Windows CMD friendly script

    @set argC=0
    @for %%x in (%*) do Set /A argC+=1
    IF %argC% == 1 (
    	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
    ) ELSE (
    	echo Usage: %0 branch_name
    )
  23. Prabir says:

    Hi,
    great article,
    anyways i was wondering, if there is a way to create an empty remote tracking branches.

  24. John says:

    Is there a way to recover a branch that is deleted from both local and remote ?

  25. Pingback: Entangled discussions » Gecko’s Guide to GIT

  26. Pingback: Marcyes / Working with remote branches « Aslak Johansen and Jan Chu @ DIKU

  27. Pingback: Creating remote git branch « Jonny Zone : Copyleft – all rights reversed

  28. Superb !!!!!!!!!!!!!
    remote branch creation is better than local branch when multiple persons working on same modules at random in different branches.

    Thanks lots

  29. Apie says:

    Just want to say that your post is listed in a private wiki for the company I work for and I reference it every now and then – thanks for the lasting contribution :)

  30. Nucc says:

    John!

    You should know a patch id from your removed branches. When you remove a branch, your patches are not removed immediately, you can remove them by using the garbage collector of git (git-gc). So you need to find one commit on the removed branch, follow the commit tree, and create a branch that refers to the last commit…

  31. Nucc says:

    I use a remote work branch to create snapshots before I change workstation (from notebook to desktop). Therefore local branches are not enough, because I’d like to sync to that “snapshot” branch from both machine…

    Thanks the article!

  32. Great article, nice to keep in bookmarks and quickly check if you need to create a new branch, thanks!

  33. kevin_mic says:

    Another useful way of creating a remote branch is by naming the topic branch you want to push to origin

    git push origin master:new_remote_branch

    or

    git push origin topic_branch:new_remote_branch

  34. Tom Lazar says:

    Like Michael Johnson I, too, prefer to create the local branch first (who knows if I really want to push it later?).

    Instead of using a .gitconfig [alias] (and having to provide the branchname manually) I simply wrote a shell script that parses the current branchname, and named it git-branch.

    #!/bin/sh
    function parse_git_branch {
      ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
      echo ${ref#refs/heads/}
    }
    BRANCHNAME=`parse_git_branch`
    git push  
    git config branch.$BRANCHNAME.remote origin
    git config branch.$BRANCHNAME.merge refs/heads/$BRANCHNAME
    
    

    More details can be found on my blog post

  35. Toby says:

    This is exactly what I was looking for cheers!

  36. Pingback: Useful Git Links | Toby's Development Blog

  37. Pingback: git的分布式开发之创建远程分支 - RubyEE.org - about Ruby,Rails,Sinatra,Mac OS & iPhone app

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 lang="" line="" escaped=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>