我怎样才能轻松地将本地Git分支推送到具有不同名称的远程? [英] How can I push a local Git branch to a remote with a different name easily?

查看:162
本文介绍了我怎样才能轻松地将本地Git分支推送到具有不同名称的远程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,是否有一种简单的方法可以用一个不同名称的远程分支来推拉本地分支,而不总是指定两个名字。



例如:

  $ git clone myrepo.git 
$ git checkout -b newb
$ ...
$ git commit -m一些更改
$ git push origin newb:remote_branch_name

现在如果有人更新了remote_branch_name,我可以:

  $ git pull 

一切都合并/快速转发。但是,如果我在本地newb中进行更改,则不能:

  $ git push 

相反,我必须:

 %git push origin newb:remote_branch_name 

看起来有点傻。如果 git-pull 使用 git-config branch.newb.merge 来确定从哪里拉,为什么不能 git-push 有类似的配置选项?有没有一个很好的捷径,或者我应该继续漫长的路程吗?

当然, 当然。只需将 push.default 设置为上游即可将分支推送至其上游(与<$ c $相同c> pull 将会从 branch.newb.merge )定义的内容拉出,而不是将分支推送到与名称匹配的分支上(这是默认的设置为 push.default 匹配)。

  git config push.default上游

请注意,这曾经是在Git 1.7.4.2之前调用跟踪不是上游,所以如果您使用的是旧版本的Git,请使用跟踪代替。在Git 1.6.4中添加了 push.default 选项,所以如果你使用的版本比这个版本旧,那么你根本没有这个选项,并且需要明确指定要推送到的分支。


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.

For example:

$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name

Now if someone updates remote_branch_name, I can:

$ git pull

And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:

$ git push

Instead, I have to:

% git push origin newb:remote_branch_name

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?

解决方案

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆