Saturday, May 23, 2009

Creating A Remote Branch With Git

If you're still doing this the way I suggested. Stop. Stop right now. Back away from the keyboard, and nobody gets hurt. Also, read the comments below, and do it the way Graham shows.

Original post:

----------------------------------------------------------------------

So, I've seen this documented in a few places, and it seems like they all give you various hard ways to do it. Here's an easy way. But, given git, it's surely not the only easy way. I create remote branches as a way to self-collaborate on a new branch from the various boxes in my daily life. If you're not a computer geek, you probably don't know that by "box" I mean "machine." And by "machine" I mean "computer." But, if you're not a computer geek, you're probably not using git, in which case you're not reading this. In that case, stop now.

Assume that you're in a clone of a remote repository. We'll create a remote branch named "mybranch." First create the branch locally:
>git branch mybranch

Switch to that branch:
>git checkout mybranch
Switched to branch "mybranch"

At this point, list all your branches:
>git branch -a
master
* mybranch
origin/HEAD
origin/master

Now, here's the command that creates the remote branch:
>git push --all
Total 0 (delta 0), reused 0 (delta 0)
To /tmp/remote.git
* [new branch] mybranch -> mybranch

It's just that easy. Now, in another clone on another machine, I can self-collaborate by creating a local branch that mirrors the remote branch. First, list all the branches:
>git branch -a
* master
origin/HEAD
origin/master
origin/mybranch

And create a local branch that tracks with the remote branch:
>git checkout -tb mybranch origin/mybranch
Branch mybranch set up to track remote branch refs/remotes/origin/mybranch.
Switched to a new branch "mybranch"

List the branches again to see the change:
>git branch -a
master
* mybranch
origin/HEAD
origin/master
origin/mybranch

Run gitk to see visually that your local branch is tracking the remote branch. Use git push and pull as you change machines to move the changes around. When the branch is done, you can merge it.

I like git. I tried bazaar and mercurial, but git "just works" and is easier to get started with than bazaar. Plus, there's github. Visit me there at https://github.com/DonBranson.

24 comments:

123 123 said...
This comment has been removed by a blog administrator.
sangprabo said...

Hi, thanks for your help. I use
$ git push --all
to make the local branch also created in the remote. But, what command I should use when I just want a particular branches to be pushed ? E.g If I have
$ git branch
* master
experimental1
experimental2

how do I push only experimental1 ? Thanks in advance

Don Branson said...
This comment has been removed by the author.
Don Branson said...

You can use the form:

git push origin <branch-name>

This assumes your remote repo's alias is 'origin', which is pretty typical.

This doesn't make the local branch a tracking branch, though, which you'll probably want. There are some good resources that might have the answer for you in this regard here http://www.kernel.org/pub/software/scm/git-core/docs/git-push.html and here http://cheat.errtheblog.com/s/git.

Nicolas Raoul said...

Thanks! I was struggling and could not understand why my branches were always local...

Don Branson said...

You're welcome! Glad it helped.

Anonymous said...

Thanks a bunch! This is precious information that I hadn't found anywhere else!

Don Branson said...

Cool! Thanks for letting me know. :)

openid said...

| Thanks a bunch! This is precious information that I hadn't found anywhere else!

+1

Kevin said...
This comment has been removed by the author.
Kevin said...

Once the local branch is pushed to the remote, execute git --set-upstream [local-branch] origin/[branch-name] to make the local branch track the remote branch.

Adam said...

An correction to Kevin's comment above: the full command is


git branch --set-upstream [local-branch] origin/[branch-name]

Don Branson said...

Kevin and Adam, thanks for adding this useful info. :)

graham said...

The way that I make a remote branch out of a local branch and set up tracking at the same time is

% git push -u :

so for example

% git push -u origin mybranch

to create "mybranch" on the remote from which the rest of the repository came and name it "mybranch" there as well. The "-u" sets up the tracking. I found this a little while ago at

http://stackoverflow.com/questions/1519006/git-how-to-create-remote-branch

Don Branson said...

Graham, that works like a champ. Thanks!

Choco said...

I love you for sharing this! I'd like to say you have no idea how difficult it is to find just this precise piece of information on git, but I suspect you do know what it's like and that's the reason you're 'saving lives' of others by sharing it. (Sorry for the rumble, I'm just very grateful!)

Don Branson said...

Choco, thanks for the kudos. :) It always feels good to be able to help somebody out.

Sarah-Jane said...

Thanks!
Why do people make things unnecessarily complicated?
:)

Anonymous said...

This post was so useful. Thanks

Gajanan said...

Thanks a lot..
This solved my problem of creation of new branch.

DomScape said...

Thanks, however Graham's suggestion doesn't work for me. I do
git push -u origin

and then get:
error: src refspec description does not match any.
error: failed to push some refs to 'git@github.com:.git'

so I just did the 'old' way :)

Anonymous said...

Thank you :)

otr214425 214425 said...

Problem: HP Printer not connecting to my laptop.
I had an issue while connecting my 2 year old HP printer to my brother's laptop that I had borrowed for starting my own business. I used a quick google search to fix the problem but that did not help me.
I then decided to get professional help to solve my problem. After having received many quotations from various companies, i decided to go ahead with Online Tech Repair (www.onlinetechrepairs.com).
Reasons I chose them over the others:
1) They were extremely friendly and patient with me during my initial discussions and responded promptly to my request.
2) Their prices were extremely reasonable.
3) They were ready and willing to walk me through the entire process step by step and were on call with me till i got it fixed.
How did they do it
1) They first asked me to state my problem clearly and asked me a few questions. This was done to detect any physical connectivity issues with the printer.
2) After having answered this, they confirmed that the printer and the laptop were functioning correctly.
3) They then, asked me if they could access my laptop remotely to troubleshoot the problem and fix it. I agreed.
4) One of the tech support executives accessed my laptop and started troubleshooting.
5) I sat back and watched as the tech support executive was navigating my laptop to spot the issue. The issue was fixed.
6) I was told that it was due to an older version of the driver that had been installed.
My Experience
I loved the entire friendly conversation that took place with them. They understood my needs clearly and acted upon the solution immediately. Being a technical noob, i sometimes find it difficult to communicate with tech support teams. It was a very different experience with the guys at Online Tech Repairs. You can check out their website www.onlinetechrepairs.com or call them on 1-914-613-3786.
Would definitely recommend this service to anyone who needs help fixing their computers.
Thanks a ton guys. Great Job....!!

otr214425 214425 said...

VIRUS REMOVAL

Is Your Computer Sluggish or Plagued With a Virus? – If So you Need Online Tech Repairs
As a leader in online computer repair, Online Tech Repairs Inc has the experience to deliver professional system optimization and virus removal.Headquartered in Great Neck, New York our certified technicians have been providing online computer repair and virus removal for customers around the world since 2004.
Our three step system is easy to use; and provides you a safe, unobtrusive, and cost effective alternative to your computer service needs. By using state-of-the-art technology our computer experts can diagnose, and repair your computer system through the internet, no matter where you are.
Our technician will guide you through the installation of Online Tech Repair Inc secure software. This software allows your dedicated computer expert to see and operate your computer just as if he was in the room with you. That means you don't have to unplug everything and bring it to our shop, or have a stranger tramping through your home.
From our remote location the Online Tech Repairs.com expert can handle any computer issue you want addressed, like:
• - System Optimization
• - How it works Software Installations or Upgrades
• - How it works Virus Removal
• - How it works Home Network Set-ups
Just to name a few.
If you are unsure of what the problem may be, that is okay. We can run a complete diagnostic on your system and fix the problems we encounter. When we are done our software is removed; leaving you with a safe, secure and properly functioning system. The whole process usually takes less than an hour. You probably couldn't even get your computer to your local repair shop that fast!
Call us now for a FREE COMPUTER DIAGONISTIC using DISCOUNT CODE (otr214425@gmail.com) on +1-914-613-3786 or chat with us on www.onlinetechrepairs.com.