• Home »
  • »
  • Creating remote branches from local ones in Git

Creating remote branches from local ones in Git

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.