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:

Anonymous said...
This comment has been removed by a blog administrator.
Prabowo Murti 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. :)

Anonymous said...

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

+1

Unknown said...
This comment has been removed by the author.
Unknown 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.

Anonymous 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 :)

dilo egg said...

I am unclear on merging fixes. Lets say I have the master branch ... a new feature branch and a debug branch. As we are building the code we fix a bunch of bugs. As bugs are fixed and committed I want to merge them into the new feature branch for testing. Can I do this and if so how?

eu4 console commands said...

learned something thank you " git push origin --delete "branch name" " added to my notes.

xcom 2 technology tree said...

I am unclear on merging fixes. Lets say I have the master branch ... a new feature branch and a debug branch. As we are building the code we fix a bunch of bugs. As bugs are fixed and committed I want to merge them into the new feature branch for testing. Can I do this and if so how?

Also, part of the file structure is the revision numbers, so I don't want to merge those files and changes ... but wish to keep the actual bug fixes. So can I be selective on which changes I merge from each bug fix branch?