git 如何将其他分支推送到远程/源?

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

How git push other branch to remote/origin?

git

提问by nfpyfzyf

My .git/config:

我的.git/config:

[remote "origin"]
    url = [email protected]:nfpyfzyf/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

My local branches:

我当地的分支机构:

                     HEAD
                     |
                 F---G  feature**current branch
                /
       C---D---E develop
      /          
 A---B  master

I'm now in feature branch, and want to push to remote. What is the current command, is itgit push origin feature? What will happen if I do git push?

我现在在功能分支,想推送到远程。当前的命令是什么,是git push origin feature吗?如果我这样做会发生什么git push

回答by Greg Bacon

To push a specific branch, run git push <remote> <branch>. In your case, your only defined remote is origin, and you want to push your featurebranch, so that makes

要推送特定分支,请运行git push <remote> <branch>. 在您的情况下,您唯一定义的遥控器是origin,并且您想推送您的feature分支,这样就可以

$ git push origin feature

The “Examples” section of the git pushdocumentationdescribes what happens if you run git pushwith no other arguments.

文档“示例”部分git push描述了如果您在git push没有其他参数的情况下运行会发生什么。

git push

Works like git push <remote>, where is the current branch's remote (or origin, if no remote is configured for the current branch).

git push

就像git push <remote>,当前分支的远程在哪里(或者origin,如果没有为当前分支配置远程)。

Given the configuration in your question, your featurebranch does not have a remote configured, so the above invocation is equivalent to the next example.

鉴于您问题中的配置,您的feature分支没有配置远程,因此上述调用等效于下一个示例。

git push origin

Without additional configuration, works like git push origin :

git push origin

没有额外的配置,就像git push origin :……

Following the daisy chain, we see that this is equivalent to

沿着菊花链,我们看到这相当于

git push origin :

Push “matching” branches to origin. See in the OPTIONSsection above for a description of "matching" branches.

git push origin :

将“匹配”分支推送到原点。有关“匹配”分支的描述,请参见上面的OPTIONS部分。

The rules for matching branches are

匹配分支的规则是

The special refspec :(or +:to allow non-fast-forward updates) directs gitto push “matching” branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side. This is the default operation mode if no explicit refspec is found (that is neither on the command line nor in any Push line of the corresponding remotes file—see below) and no push.defaultconfiguration variable is set.

特殊的 refspec :(或+:允许非快进更新)指示git推送“匹配”分支:对于本地端存在的每个分支,如果远程端已经存在同名分支,则更新远程端. 如果未找到明确的 refspec(即既不在命令行上也不在相应远程文件的任何 Push 行中,见下文)并且未push.default设置配置变量,则这是默认操作模式。

In your case, the only matching branch is master, so git pushwill push that branch and exit.

在您的情况下,唯一匹配的分支是master,因此git push将推动该分支并退出。

回答by Patrick B.

Yes git push origin featureis the right and explicit command.

Yesgit push origin feature是正确且明确的命令。

What happens when you do git pushwithout arguments is influenced by your configuration. The configuration variable push.defaulttells git what to do in this case. Check the man-page (git config --helpand search for push.default) for more information. Just so much, there are several possibilities: nothing, matching, upstream, simpleand current.

git push没有参数时会发生什么会受到您的配置的影响。配置变量push.default告诉 git 在这种情况下要做什么。查看手册页(git config --help并搜索push.default)以获取更多信息。只是这么多,有几种可能:nothingmatchingupstreamsimplecurrent

Also What is the difference between git push.default=current and push.default=upstream?can help.

另外git push.default=current 和 push.default=upstream 有什么区别?可以帮助。

回答by 1615903

git push origin featureis correct. If you do just git pushit will probably say something like this:

git push origin feature是正确的。如果你这样做,git push它可能会说这样的话:

fatal: The current branch feature has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature

After you have used the --set-upstream(or just -u), simple git pushwill work

使用--set-upstream(或仅使用 -u)后, simplegit push将起作用

回答by alexclooze

If you're working with git-flowyou can use

如果您正在使用git-flow,则可以使用

git flow feature publish $FEATURE

to push the branch to your remote and create it remote.

将分支推送到您的远程并创建远程。

It's equal to do a simple

等于做一个简单的

git push origin $FEATURE

If you're simply doing a git pushit depends on your configuration what happens - in my configuration it pushes all remotely existing branches.

如果你只是做一个git push它取决于你的配置会发生什么 - 在我的配置中它推送所有远程现有的分支。

回答by John Szakmeister

git push origin $FEATUREis certainly correct, and will get you what you want. However, I often use:

git push origin $FEATURE肯定是正确的,会得到你想要的。但是,我经常使用:

git push -u origin HEAD

This will push up my current branch, using the branch name, so I don't have to type it out explicitly on the command line. As mentioned in one of the other answers, -uwill set the upstream so that a plain git pushwill work in the future.

这将使用分支名称推送我当前的分支,因此我不必在命令行上明确键入它。如其他答案之一所述,-u将设置上游,以便平原git push在未来工作。