Sharing Git Branches Remotely
Sunday, May 1, 2011 at 1:11PM I usually have two branches when working on a project with a team. The master branch tracks origin/master, and I do most of my work on my local development branch.
Every now and then, I work an experimental feature. When I start on it, I'm not sure if it will work out and wether or not it should make it into the master branch someday. In those cases, I will make a new local branch to play with the idea. Lets say I called it superCoolFeature. At some point of the development of this feature, I might need to share this branch with one of my team members. How do do I do that?
Well... I just figured that out recently and I thought I'd post it here in order to remember it the next time I need it.
It turns out to be a two step process. First you need to push your local branch onto your remote (usually your remote is origin, but you can type git remote to see a list of remotes.) After that, you will need to make your local branch track your remote branch. Here are the commands that do just that:
Note: You need to be on a different branch before you execute the second command, so you might want to do a git checkout master
first.
git push origin <branch>
git branch -f <branch> origin/<branch>
or to be more specific to my example:
git push origin superCoolFeature
git branch -f superCoolFeature origin/superCoolFeature
Once you've done that, you should have a new branch on your remote and your local branch should track it. Now you need to tell your teammate to create his own local branch that tracks the remote branch. They can do that with the following:
git branch -t <branch> origin/<branch>
or again, more specifically:
git branch -t superCoolFeature origin/superCoolFeature
As always, you should proceed with caution. Have backups of your repo before performing a new command for the first time (In general backups are a good idea, regardless of what you're up to.)
Also for future reference, here is the list of blog post on the subject I found while investigating this:
- http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/
- http://coders-log.blogspot.com/2009/05/creating-remote-branch-with-git.html
- http://markelikalderon.com/2008/08/26/pushing-and-tracking-remote-branches-with-git/
- http://gitready.com/beginner/2009/03/09/remote-tracking-branches.html
git
Reader Comments (1)
Thanks Jose!
I'll try this out. I was doing this more manually without tracking.
So I just push/fetch to/from the branch I want. But I have to be very careful so it may lead to mistakes. Tracking looks better.