如何从本地分支“X”隐式“git push”至“原点/Y”

发布于 2024-12-06 23:18:56 字数 766 浏览 0 评论 0原文

假设您的源站上有一个分支,其名称长得可笑……

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

当您在本地处理该分支时,您想给它一个不那么荒谬的名称,例如 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

当需要推送时,你能做什么,如果你运行:

$> git checkout bob
$> git push

那么“bob”上的任何本地更改都将被发送到“branch-with-a-ridiculously-long-name”,并且不会创建origin 上的新分支称为“bob”?

我实际上正在寻找一种使 git Push 隐式扩展为 git Push origin bob:branch-with-a-ridiculously-long-name 的方法。

我认为设置 git config push.default upload 可以部分解决问题,但我不确定如何处理本地分支的名称与远程分支的名称不同的事实。

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

gt; 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.

gt; git checkout origin/branch-with-a-ridiculously-long-name
gt; git checkout -b bob
gt; 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:

gt; git checkout bob
gt; 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-12-13 23:18:56

如果您将 push.default 设置为 upstream (或在 1.7.4.2 之前的 git 版本中设置 tracking),那么它应该完全符合您的要求您运行:

   git push

... 或:

   git push origin

您运行的 gitbranch --set-upstream 命令与配置设置相结合,应该可以实现该功能。

我写了一篇关于这个的文章不幸的是 git push 和 git pull 之间的不对称

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.

萌︼了一个春 2024-12-13 23:18:56

最新版本的 git(大多数 2.x 版本)包含在一个命令中设置所有此配置的选项:

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

这会将 bob 的上游设置为正确的远程分支。

或者,如果您已经有本地分支,则可以使用 --set-upstream-to 标志:

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

这两者都将正确设置 git 配置

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文