如何轻松地将本地 Git 分支推送到具有不同名称的远程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5738797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:42:20  来源:igfitidea点击:

How can I push a local Git branch to a remote with a different name easily?

gitversion-controlgit-pushgit-remote

提问by jmacdonagh

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:

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

$ git pull

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

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

$ git push

Instead, I have to:

相反,我必须:

% git push origin newb:remote_branch_name

Seems a little silly. If git-pulluses git-config branch.newb.mergeto determine where to pull from, why couldn't git-pushhave a similar config option? Is there a nice shortcut for this or should I just continue the long way?

好像有点傻。如果git-pull用于git-config branch.newb.merge确定从何处拉取,为什么不能git-push有类似的配置选项?对此有什么好的捷径,还是我应该继续走很长的路?

采纳答案by Brian Campbell

Sure. Just set your push.defaultto upstreamto push branches to their upstreams (which is the same that pullwill 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).

当然。只需设置你push.defaultupstream推分行他们的上行流(这是相同的pull,带坏,由定义branch.newb.merge),而不是推动分支机构在那些名称匹配(这是默认设置push.defaultmatching)。

git config push.default upstream

Note that this used to be called trackingnot upstreambefore Git 1.7.4.2, so if you're using an older version of Git, use trackinginstead. The push.defaultoption 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.

请注意,这曾经tracking不是upstream在 Git 1.7.4.2 之前调用的,因此如果您使用的是旧版本的 Git,请tracking改用。该push.default选项是在 Git 1.6.4 中添加的,因此如果您使用的是比该版本更旧的版本,则根本没有此选项,并且需要明确指定要推送到的分支。

回答by Adam Dymitruk

When you do the initial push add the -u parameter:

当您进行初始推送时,添加-u 参数

git push -u origin my_branch:remote_branch

Subsequent pushes will go where you want.

随后的推送将去到你想要的地方。

EDIT:

编辑:

As per the comment, that only sets up pull.

根据评论,这只会设置拉力。

git branch --set-upstream

should do it.

应该这样做。

回答by jobin

The command by Adamis now deprecated. You can use:

Adam的命令现已弃用。您可以使用:

git branch --set-upstream-to origin/my_remote_branch my_local_branch

to set the upstream branch of my_local_branchto origin/my_remote_branch.

将上游分支设置my_local_branchorigin/my_remote_branch

回答by urubuz

Here's the process that has worked for me.

这是对我有用的过程。

git clone original-repo-url
git remote rename origin upstream
git remote add origin new-repo-url

Now your new repo will be ‘origin' and the original repo is ‘upstream'. Confirm it by running git remote -v. (Side note: Upstream is used to fetch from the original repo - in order to keep your local copy in sync with the project you want to contribute to - and origin is used to pull and push since you can contribute to your own repo).

现在您的新仓库将是“起源”,而原始仓库是“上游”。通过运行 git remote -v 进行确认。(旁注:上游用于从原始存储库中获取 - 为了使您的本地副本与您想要贡献的项目保持同步 - 源用于拉取和推送,因为您可以为自己的存储库做出贡献)。

git push origin master

git push origin master

Now your new remote repo's master (on Github) will be in-sync with the original master, but it won't have any of the feature branches.

现在你的新远程仓库的 master(在 Github 上)将与原始 master 同步,但它不会有任何功能分支。

git rebase upstream/branch-name
git push origin master

Rebase is a smart merge. Then push to master again and you'll see the selected feature branch as master on the new repo.

Rebase 是一个智能合并。然后再次推送到 master,您将在新 repo 上看到所选的功能分支作为 master。

Optional:

可选的:

git remote rm upstream
git remote add upstream new-repo-url

回答by messmania

I have been running into the same issue for quite sometime now. I finally have a set of statements so I don't have to do git push origin local:remoteevery time. I followed these:

我已经遇到了同样的问题很长一段时间了。我终于有了一套陈述,所以我不必git push origin local:remote每次都这样做。我遵循了这些:

git branch --set-upstream-to origin/remote_branch_name
git config push.default upstream
git push

After setting upstream to a remote branch with different name (1st line) and then making that upstream as default (2nd line), 3rd line will now obey these rules and push to the set upstream.

将上游设置为具有不同名称的远程分支(第一行),然后将该上游设置为默认值(第二行)后,第三行现在将遵守这些规则并推送到上游设置。