« triggerGreppy | Main | Kinesis Advantage Keyboard - First Impressions »
Sunday
May012011

Sharing Git Branches Remotely

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:

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (9)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: ceeFitness
  • Response
    Response: Xovilichterr
    Little Mustard - Blog - Sharing Git Branches Remotely
  • Response
    Little Mustard - Blog - Sharing Git Branches Remotely
  • Response
    Response: ereader
    Little Mustard - Blog - Sharing Git Branches Remotely
  • Response
    I often get struck in giving commands, most of the time I made mistakes in the code. And forget to have back up which really screws me up. I am so pleased through your post, as it’s the perfect solution for my problem, you have explained deliberately.
  • Response
    I found a great...
  • Response
    I have tried this commands to perfrom but i failed to perform the task. Just getting into another way on how to deal with it for the next process i do. And looking forward to it. GLad for the code presented ehre baove so helpful to me to try these commands ...
  • Response
    Response: SilagraPills
    The internet can make it simplier and easier than at any time to acquire foods distribution providers.� Simply because they are on-line, they are easy for you to learn. You can shop for what you desire suitable from your unique personal computer. It is less complicated than actually to uncover food ...
  • Response
    Response: net worth

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.

May 1, 2011 | Unregistered CommenterJose Lobato

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>