Start a New Branch on your Remote Git Repository

by Geoff Lane on April 14th, 2008

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:

From → Automation, Code

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

  2. pietro permalink

    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

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

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

  5. Pavan Mishra permalink

    Quite helpful and concise. Thanks.

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

  7. You can also delete a remote branch with:

    git branch -r -d origin/new_feature_name

  8. Michael Johnson permalink

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

  9. Michael Johnson permalink

    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.

  10. kbro permalink

    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.

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

  12. Thanks for this bro.

  13. 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
  14. Vivek permalink

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

  15. skiedr permalink

    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
    )
  16. Hi,
    great article,
    anyways i was wondering, if there is a way to create an empty remote tracking branches.

  17. John permalink

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

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

    Thanks lots

  19. Apie permalink

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

  20. 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…

  21. 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!

Trackbacks & Pingbacks

  1. Working with remote branches « Aslak Johansen and Jan Chu @ DIKU
  2. Vertical Visions :: A technical Blog about Scripting, Monitoring and so on...
  3. Tims Blog » Blog Archive » Time to use vacation
  4. Creating remote branches from local ones in Git | Question Defense
  5. Working with remote GIT branches « Aslak Johansen and Jan Chu @ DIKU
  6. djwonk › Tracking Remote Git Branches
  7. When in doubt… › Creating (and removing) remote Git branches
  8. Entangled discussions » Gecko’s Guide to GIT
  9. Marcyes / Working with remote branches « Aslak Johansen and Jan Chu @ DIKU
  10. Creating remote git branch « Jonny Zone : Copyleft – all rights reversed

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS