3

Say you have a branch on your origin that has a ridiculously long name...

$> git branch -a
* master
  origin/master
  origin/branch-with-a-ridiculously-long-name

And when you work on that branch locally, you want to give it a less ridiculous name, like bob.

$> git checkout origin/branch-with-a-ridiculously-long-name
$> git checkout -b bob
$> git branch --set-upstream bob origin/branch-with-a-ridiculously-long-name

When it comes time to push, what can you do such that if you run:

$> git checkout bob
$> git push

then any local changes on "bob" will be sent to the "branch-with-a-ridiculously-long-name", and won't create a new branch on origin called "bob"?

I'm effectively after a way of making git push implicitly expand in to git push origin bob:branch-with-a-ridiculously-long-name.

I think setting git config push.default upstream goes part of the way, but I'm not sure how to deal with the fact that the local branch's name differs from the remote.

Chris
  • 8,973
  • 3
  • 26
  • 31
  • I think this question has been asked before at http://stackoverflow.com/questions/4109136/configure-a-local-branch-for-push-to-specific-branch – Chris Sep 28 '11 at 14:15

3 Answers3

3

If you set push.default to upstream (or tracking in versions of git before 1.7.4.2), that should do exactly what you want when you run:

   git push

... or:

   git push origin

The git branch --set-upstream command that you ran, in combination with the config setting, should make that work.

I wrote a post about this unfortunate asymmetry between git push and git pull.

Mark Longair
  • 385,867
  • 66
  • 394
  • 320
  • Oh man, I was basically at the solution, but I didn't realise it! Thanks, Mark, `push.default` is the secret sauce I needed :) – Chris Sep 28 '11 at 12:44
1

Is this what you're after? http://markmcb.com/2008/09/21/multiple-remote-git-branches-with-different-local-names/

JamesHalsall
  • 12,322
  • 4
  • 36
  • 64
  • That's a pretty inventive approach to solving the problem! I'm hoping there's some nice first-order support that doesn't involve manually editing the config files, but this is good to know. Thanks! – Chris Sep 28 '11 at 12:43
0

More recent versions of git (most 2.x versions) include the option to set all of this configuration in one command:

git checkout -b bob origin/branch-with-a-ridiculously-long-name

That will set the upstream of bob to the correct remote branch.

Alternatively if you have a local branch already, you can use the --set-upstream-to flag:

git checkout bob
git branch --set-upstream-to origin/branch-with-a-ridiculously-long-name

Both of these will correctly set the git config

Chris
  • 8,973
  • 3
  • 26
  • 31