Multiple Remote Git Branches With Different Local Names
I pounded my head against the wall for a bit when trying to play out this scenario in Git:
- Remote repository has two branches: master and some-long-complex-name
- Locally, I have cloned master
- I have set up my config to refer to the remote master as “origin”
- I have checked out some-long-complex-name using the following:
$ git checkout --track -b simple-name origin/some-long-complex-name
The key thing to note is that my local branch has a different name than the remote branch, i.e., “simple-name” is my local branch that’s tracking the remote branch “some-long-complex-name”. I’ve used pseudo-branch-names in this example, but in practice I like my local branch names to be 3 characters or so such that they’re easy to type, and I like the remote branches to have long names such that there is no ambiguity about what they are.
My initial .git/config looked like this after the aforementioned checkout command:
[remote "origin"] url = ssh://myserv/srv/git/proj.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [branch "simple-name"] remote = origin merge = refs/heads/some-long-complex-name
Now, this isn’t too bad. All pull related commands work, but push only works for the master branch. What I wanted was git to push to “some-long-complex-name” whenever I ran “git push” from my local “simple-name” branch.
I changed .git/config to look like this:
[remote "origin"] url = ssh://myserv/srv/git/proj.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin [remote "simple_origin"] url = ssh://myserv/srv/git/proj.git fetch = +refs/heads/*:refs/remotes/origin/* push = simple-name:some-long-complex-name [branch "simple-name"] remote = simple_origin merge = refs/heads/some-long-complex-name
Note the additional “remote” section and the “push” reference. Now, when I’m in my simple-name branch, I can just type “git push” and this branch will push out to the remote branch names “some-long-complex-name”.
Thanks to those who offered help on the freenode IRC #git channel, namely RandalSchwartz.
October 10th, 2008 at 3:20 pm
Thanks for the tip. I’d actually just about consider it a bug that push doesn’t work that way by default, thus breaking pull/push symmetry.
July 17th, 2009 at 6:30 am
Thanks a lot for this tip. I just spent the last half an hour banging my head over this too.
Btw any reason you created another remote “simple_origin”? I just added that “push =” line to my “origin” itself and it worked.
July 17th, 2009 at 2:16 pm
@Harish, At the time I wrote this I think there was a bug in git that forced me to do that. You are correct though, it is no longer necessary.
September 22nd, 2009 at 6:26 pm
thanks a lot. i was trying to figure out a couple of hours why the tracking branch keeps trying to push the master branch even i am in another local branch… well done!
February 2nd, 2010 at 9:16 am
Dude, thanks! Was searching for quite a while for this! Don’t quite understand why you use simple_origin instead of just putting the push line into the original remote section though? Anyway, great!