git Git推送到远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36139275/
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-09 04:03:13  来源:igfitidea点击:

Git pushing to remote branch

git

提问by Em Ae

I tried to follow this postbut got confused rather than getting my problem solved.

我试图关注这篇文章,但感到困惑而不是解决我的问题。

Here is the scenario.

这是场景。

I have created a branch like git checkout -b <branch_name>. I then worked on some changes and did

我创建了一个像git checkout -b <branch_name>. 然后我做了一些改变并做了

  1. git add .
  2. git commit -m "comment"
  1. git add .
  2. git commit -m "comment"

Now all those changes are in my uncommitted local <branch_name>branch. What i want to do is to push these changes upstream but i want to push it to a branch which doesn't exist e.g., <branch_name>_test

现在所有这些更改都在我未提交的本地<branch_name>分支中。我想要做的是将这些更改推送到上游,但我想将其推送到不存在的分支,例如,<branch_name>_test

I don't want to push it to my <branch_name>. How can i do so.

我不想把它推到我的<branch_name>. 我怎么能这样做。

回答by torek

First, let's note that git push"wants" two more arguments and will make them up automatically if you don't supply them. The basic command is therefore git push remoterefspec.

首先,让我们注意git push“想要”另外两个参数,如果你不提供它们,它们会自动组成。因此,基本命令是.git push remoterefspec

The remotepart is usually trivial as it's almost always just the word origin. The trickier part is the refspec. Most commonly, people write a branch name here: git push origin master, for instance. This uses your local branch to push to a branch of the same name1on the remote, creating it if necessary. But it doesn't have to be justa branch name.

remote部分通常是微不足道的,因为它几乎总是只是这个词origin。更棘手的部分是refspec. 最常见的是,人们在这里写一个分支名称:git push origin master例如。这将使用您的本地分支推送到远程上的同名分支1,并在必要时创建它。但它不必只是一个分支名称。

In particular, a refspechas two colon-separated parts. For git push, the part on the left identifies what to push,2and the part on the right identifies the name to give to the remote. The part on the left in this case would be branch_nameand the part on the right would be branch_name_test. For instance:

特别是, arefspec有两个冒号分隔的部分。因为git push,左侧的部分标识要推送的内容2右侧的部分标识要提供给遥控器的名称。在这种情况下,左边branch_name的部分是 ,右边的部分是branch_name_test。例如:

git push origin foo:foo_test

As you are doing the push, you can tell your git pushto set your branch's upstream nameat the same time, by adding -uto the git pushoptions. Setting the upstream name makes your git save the foo_test(or whatever) name, so that a future git pushwith no arguments, while you're on the foobranch, can try to push to foo_teston the remote (git also saves the remote, originin this case, so that you don't have to enter that either).

当你在做推,你可以告诉你git push设置你的分支的上游名字的同时,加入-ugit push选项。设置上游名称会使您的 git 保存foo_test(或其他)名称,以便git push没有参数的未来,当您在foo分支上时,可以尝试推送到foo_test远程(origin在这种情况下,git 也保存远程,这样你也不必输入)。

You need only pass -uonce: it basically just runs git branch --set-upstream-tofor you. (If you pass -uagain later, it re-runs the upstream-setting, changing it as directed; or you can run git branch --set-upstream-toyourself.)

你只需要通过-u一次:它基本上只是git branch --set-upstream-to为你运行。(如果您-u稍后再次通过,它会重新运行上游设置,按照指示进行更改;或者您可以git branch --set-upstream-to自己运行。)

However, if your git is 2.0 or newer, and you have not set any special configuration, you will run into the same kind of thing that had me enter footnote 1 above: push.defaultwill be set to simple, which will refuse to push because the upstream's name differs from your own local name. If you set push.defaultto upstream, git will stop complaining—but the simplest solution is just to rename your local branch first, so that the local and remote names match. (What settings to set, and/or whether to rename your branch, are up to you.)

但是,如果您的 git 是 2.0 或更新版本,并且您没有设置任何特殊配置,您将遇到与我在上面输入脚注 1 相同的事情:push.default将设置为simple,这将拒绝推送,因为上游的名称与您自己的本地名称不同。如果您设置push.defaultupstream,git 将停止抱怨——但最简单的解决方案是先重命名您的本地分支,以便本地和远程名称匹配。(设置什么设置,和/或是否重命名您的分支,由您决定。)



1More precisely, git consults your remote.remote.pushsetting to derive the upstream half of the refspec. If you haven't set anything here, the defaultis to use the same name.

1更准确地说,git 会参考您的设置以导出 refspec 的上游一半。如果您没有在此处设置任何内容,则默认使用相同的名称。remote.remote.push

2This doesn't have to be a branch name. For instance, you can supply HEAD, or a commit hash, here. If you use something other than a branch name, you mayhave to spell out the full refs/heads/branchon the right, though (it depends on what names are already on the remote).

2这不必是分支名称。例如,您可以HEAD在此处提供或提交哈希。如果您使用分支名称以外的其他名称,则可能必须在右侧拼出完整名称(这取决于遥控器上已有的名称)。refs/heads/branch

回答by Awakening Byte

Simply push this branch to a different branch name

只需将此分支推送到不同的分支名称

 git push -u origin localBranch:remoteBranch

回答by Scott

git push --set-upstream origin <branch_name>_test

git push --set-upstream origin <branch_name>_test

--set-upstreamsets the association between your local branch and the remote. You only have to do it the first time. On subsequent pushes you can just do:

--set-upstream设置本地分支和远程之间的关联。你只需要第一次做。在后续推送中,您可以执行以下操作:

git push

git push

If you don't have originset yet, use:

如果您还没有origin设置,请使用:

git remote add origin <repository_url>then retry the above command.

git remote add origin <repository_url>然后重试上面的命令。

回答by TerraPass

You can push your local branch to a newremote branch like so:

您可以将本地分支推送到新的远程分支,如下所示:

git push origin master:test

(Assuming originis your remote, masteris your local branch name and testis the name of the new remote branch, you wish to create.)

(假设origin是您的远程,master是您的本地分支名称,test是您希望创建的新远程分支的名称。)

If at the same time you want to set up your local branch to track the newly created remote branch, you can do so with -u(on newer versions of Git) or --set-upstream, so:

如果同时要设置本地分支以跟踪新创建的远程分支,可以使用-u(在较新版本的 Git 上)或--set-upstream,因此:

git push -u origin master:test

or

或者

git push --set-upstream origin master:test

...will create a new remote branch, named test, in remote repository origin, based on your local master, andsetup your local masterto track it.

...将test在远程存储库origin中基于您的本地创建一个名为 的新远程分支master设置您的本地master来跟踪它。

回答by Makoto

With modern Git versions, the command to use would be:

对于现代 Git 版本,要使用的命令是:

git push -u origin <branch_name_test>

This will automatically set the branch name to track from remote and push in one go.

这将自动设置分支名称以从远程跟踪并一次性推送。