how to implicitly `git push` from a local branch “X” to “origin/Y”

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 20:47:50

问题


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.


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/7583091/how-to-implicitly-git-push-from-a-local-branch-x-to-origin-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!