I had an existing local branch that I had done some commits to and wanted to create a remote branch on origin so that I could check it out to other local branches on different machines. The current version of Git (1.6.1.x at the time of this writing), doesn’t seem to have an elegant way of doing this, but still I was able to do it fairly simply by manually editing Git’s “config” file (inside of the “.git” folder of a working directory).I first created a remote branch (this assumes that I already had a local branch named “new_feature_name”):
git push origin origin:refs/heads/new_feature_name

Then, I edited the config file and added an entry to connect the local branch to the remote branch:
[branch "new_feature_name"]
remote = origin
merge = refs/heads/new_feature_name

You can also do this with git config by:
git config branch.new_feature_name.remote origin
git config branch.new_feature_name.merge refs/heads/new_feature_name

I then checked out the local branch and tried to push it to the remote one with:
git push origin new_feature_name
but got some kind of error about divergent branches, so I pulled the remote branch to the local one with:
git pull origin new_feature_name
and this seemed to merge the two together well enough so that I could then do a:
git push

On another machine, I then did a:
git pull
and created a new local branch that tracked the remote one with:
git checkout --track -b new_feature_name origin/new_feature_name

Thanks to this post for examples of setting up remote branches.

DeliciousStumbleUponDiggTwitterFacebookRedditLinkedInEmail
Tags: , ,
2 Responses to “Creating remote branches from local ones in Git”
  1. Chirag Patel says:

    If you have Rubygems installed, you can install git_remote_branch to manage remote branches very easily

    gem install git_remote_branch

    grb track remote_branch
    grb create remote_branch
    grb delete remote_branch
    grb rename remote_branch

    Also, use “git remote prune origin” to refresh the remote branch list

    [Reply]

  2. derek says:

    Grrreat! This looks much more elegant.

    [Reply]

  3.  
Leave a Reply

*Type the letter/number combination in the abvoe field before clicking submit.

*