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:
----------------------------------------------------------------------
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:
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
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.
Thanks! I was struggling and could not understand why my branches were always local...
You're welcome! Glad it helped.
Thanks a bunch! This is precious information that I hadn't found anywhere else!
Cool! Thanks for letting me know. :)
| Thanks a bunch! This is precious information that I hadn't found anywhere else!
+1
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.
An correction to Kevin's comment above: the full command is
git branch --set-upstream [local-branch] origin/[branch-name]
Kevin and Adam, thanks for adding this useful info. :)
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
Graham, that works like a champ. Thanks!
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!)
Choco, thanks for the kudos. :) It always feels good to be able to help somebody out.
Thanks!
Why do people make things unnecessarily complicated?
:)
This post was so useful. Thanks
Thanks a lot..
This solved my problem of creation of new branch.
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 :)
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?
learned something thank you " git push origin --delete "branch name" " added to my notes.
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?
Post a Comment