如何轻松地将本地 Git 分支推送到具有不同名称的远程? [英] How can I push a local Git branch to a remote with a different name easily?
问题描述
我一直想知道是否有一种简单的方法可以使用不同名称的远程分支推送和拉取本地分支,而无需始终指定两个名称.
I've been wondering if there's an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.
例如:
$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name
现在如果有人更新 remote_branch_name,我可以:
Now if someone updates remote_branch_name, I can:
$ git pull
一切都被合并/快进.但是,如果我在本地newb"中进行更改,则不能:
And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:
$ git push
相反,我必须:
% git push origin newb:remote_branch_name
看起来有点傻.如果 git-pull
使用 git-config branch.newb.merge
来确定从哪里拉取,为什么 git-push
不能有一个类似的配置选项?对此有什么好的捷径,还是我应该继续走很长的路?
Seems a little silly. If git-pull
uses git-config branch.newb.merge
to determine where to pull from, why couldn't git-push
have a similar config option? Is there a nice shortcut for this or should I just continue the long way?
推荐答案
好的.只需将您的 push.default
设置为 upstream
以将分支推送到它们的上游(这与 pull
将从中拉出的相同,由 branch.newb.merge
),而不是将分支推送到名称匹配的分支(这是push.default
、matching
的默认设置).
Sure. Just set your push.default
to upstream
to push branches to their upstreams (which is the same that pull
will pull from, defined by branch.newb.merge
), rather than pushing branches to ones matching in name (which is the default setting for push.default
, matching
).
git config push.default upstream
请注意,在 Git 1.7.4.2 之前,这曾经被称为 tracking
而不是 upstream
,因此如果您使用的是旧版本的 Git,请使用 tracking
代替.push.default
选项是在 Git 1.6.4 中添加的,因此如果您使用的是比该版本更旧的版本,则根本没有此选项,并且需要明确指定分支以推到.
Note that this used to be called tracking
not upstream
before Git 1.7.4.2, so if you're using an older version of Git, use tracking
instead. The push.default
option was added in Git 1.6.4, so if you're on an older version than that, you won't have this option at all and will need to explicitly specify the branch to push to.
这篇关于如何轻松地将本地 Git 分支推送到具有不同名称的远程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!