没有指定分支的“git push”的默认行为

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

Default behavior of "git push" without a branch specified

gitbranchgit-branchgit-push

提问by PlagueHammer

I use the following command to push to my remote branch:

我使用以下命令推送到我的远程分支:

git push origin sandbox

If I say

如果我说

git push origin

does that push changes in my other branches too, or does it only update my current branch? I have three branches: master, productionand sandbox.

这是否也会推动我的其他分支中的更改,还是仅更新我当前的分支?我有三个分支:master,productionsandbox

The git pushdocumentation is not very clear about this, so I'd like to clarify this for good.

git push文件是不是很清楚这一点,所以我想澄清这一点为好。

Which branches and remotes do the following git pushcommands update exactly?

以下git push命令会准确更新哪些分支和遥控器?

git push 
git push origin

originabove is a remote.

origin上面是遥控器。

I understand that git push [remote] [branch]will push only that branch to the remote.

我知道这git push [remote] [branch]只会将该分支推送到远程。

回答by Brian L

You can control the default behavior by setting push.default in your git config. From the git-config(1) documentation:

您可以通过在 git 配置中设置 push.default 来控制默认行为。来自git-config(1) 文档

push.default

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

定义 git push 应该采取的操作,如果命令行上没有给出 refspec,远程中没有配置 refspec,并且命令行上给出的任何选项都没有暗示任何 refspec。可能的值为:

  • nothing: do not push anything

  • matching: push all matching branches

    All branches having the same name in both ends are considered to be matching.

    This used to be the default, but not since Git 2.0 (simpleis the new default).

  • upstream: push the current branch to its upstream branch (trackingis a deprecated synonym for upstream)

  • current: push the current branch to a branch of the same name

  • simple: (new in Git 1.7.11) like upstream, but refuses to push if the upstream branch's name is different from the local one

    This is the safest option and is well-suited for beginners.

    This mode has become the default in Git 2.0.

  • nothing: 不要推任何东西

  • matching: 推送所有匹配的分支

    两端具有相同名称的所有分支都被认为是匹配的。

    这曾经是默认值,但不是因为 Git 2.0(simple是新的默认值)。

  • upstream: 将当前分支推送到其上游分支(tracking是不推荐使用的上游同义词)

  • current: 将当前分支推送到同名分支

  • simple: (Git 1.7.11 新增) 像上游一样,但如果上游分支的名称与本地分支名称不同,则拒绝推送

    这是最安全的选择,非常适合初学者。

    此模式已成为 Git 2.0 中的默认模式。

The simple, current and upstream modes are for those who want to push out a single branch after finishing work, even when the other branches are not yet ready to be pushed out

simple、current 和 upstream 模式适用于那些希望在完成工作后推出单个分支的人,即使其他分支尚未准备好推出

Command line examples:

命令行示例:

To view the current configuration:

查看当前配置:

git config --global push.default

To set a new configuration:

要设置新配置:

git config --global push.default current

回答by Christoffer

You can set up default behavior for your git with push.default

您可以使用 push.default 为您的 git 设置默认行为

git config push.default current

or if you have many repositories and want the same for all then

或者如果您有许多存储库并且希望所有存储库都相同

git config --global push.default current

The currentin this setup means that by default you will only push the current branchwhen you do git push

此设置中的当前意味着默认情况下,您只会在执行git push推送当前分支

Other options are:

其他选项是:

  • nothing : Do not push anything
  • matching : Push all matching branches (default)
  • tracking : Push the current branch to whatever it is tracking
  • current : Push the current branch
  • 没有:不要推动任何东西
  • 匹配:推送所有匹配的分支(默认)
  • 跟踪:将当前分支推送到它正在跟踪的任何地方
  • current : 推送当前分支


UPDATE - NEW WAY TO DO THIS

更新 - 执行此操作的新方法

As of Git 1.7.11 do the following:

从 Git 1.7.11 开始,请执行以下操作:

git config --global push.default simple

This is a new setting introduced that works in the same way as current, and will be made default to git from v 2.0 according to rumors

这是一个引入的新设置,其工作方式与当前相同,并且根据传言将默认为 v 2.0 中的 git

回答by baudtack

git push originwill push all changes on the local branches that have matching remote branches at originAs for git push

git push origin将在具有匹配远程分支的本地分支上推送所有更改originAs forgit push

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

就像git push <remote><remote>当前分支的远程(或原点,如果没有为当前分支配置远程)的位置。

From the Examples section of the git-pushman page

来自手册页的示例部分git-push

回答by neoneye

I just committed my code to a branch and pushed it to github, like this:

我只是将我的代码提交到一个分支并将其推送到 github,如下所示:

git branch SimonLowMemoryExperiments
git checkout SimonLowMemoryExperiments
git add .
git commit -a -m "Lots of experimentation with identifying the memory problems"
git push origin SimonLowMemoryExperiments

回答by Mukesh Chapagain

Here is a very handy and helpful information about Git Push: Git Push: Just the Tip

这里有一个关于Git Push的非常方便和有用的信息: Git Push: Just the Tip

The most common use of git push is to push your local changes to your public upstream repository. Assuming that the upstream is a remote named "origin" (the default remote name if your repository is a clone) and the branch to be updated to/from is named "master" (the default branch name), this is done with: git push origin master

git push 最常见的用途是将您的本地更改推送到您的公共上游存储库。假设上游是一个名为“origin”的远程(如果您的存储库是克隆,则为默认远程名称)并且要更新到/从的分支名为“master”(默认分支名称),这是通过以下方式完成的:git push origin master

git push originwill push changes from all local branches to matching branches the origin remote.

git push origin将所有本地分支的更改推送到源远程匹配的分支。

git push origin masterwill push changes from the local master branch to the remote master branch.

git push origin master将更改从本地 master 分支推送到远程 master 分支。

git push origin master:stagingwill push changes from the local master branch to the remote staging branch if it exists.

git push origin master:staging如果存在,会将更改从本地主分支推送到远程暂存分支。

回答by VonC

(March 2012)
Beware: that default "matching" policy might change soon
(sometimes after git1.7.10+)
:

(2012 年 3 月)
注意:默认的“ matching”政策可能很快就会改变
(有时在 git1.7.10+ 之后)

See "Please discuss: what "git push" should do when you do not say what to push?"

请参阅“请大家讨论:什么是‘混帐推’当你不说推我应该怎么办?

In the current setting(i.e. push.default=matching), git pushwithout argument will push all branches that exist locally and remotely with the same name.
This is usually appropriate when a developer pushes to his own public repository, but may be confusing if not dangerous when using a shared repository.

The proposal is to change the default to 'upstream', i.e. push only the current branch, and push it to the branch git pull would pull from.
Another candidate is 'current'; this pushes only the current branch to the remote branch of the same name.

What has been discussed so far can be seen in this thread:

在当前设置(即push.default=matching)中,git push不带参数将推送本地和远程存在的所有同名分支
当开发人员推送到他自己的公共存储库时,这通常是合适的,但在使用共享存储库时,如果没有危险,可能会造成混淆。

建议是将默认值更改为 ' upstream',即仅推送当前分支,并将其推送到 git pull 将从中拉出的分支。
另一个候选人是' current'; 这只会将当前分支推送到同名的远程分支。

到目前为止讨论的内容可以在这个线程中看到:

http://thread.gmane.org/gmane.comp.version-control.git/192547/focus=192694

http://thread.gmane.org/gmane.comp.version-control.git/192547/focus=192694

Previous relevant discussions include:

之前的相关讨论包括:

To join the discussion, send your messages to: [email protected]

要加入讨论,请将您的消息发送至:[email protected]

回答by Mat Schaffer

I just put this in my .gitconfig aliases section and love how it works:

我只是把它放在我的 .gitconfig aliases 部分并且喜欢它的工作原理:

pub = "!f() { git push -u ${1:-origin} `git symbolic-ref HEAD`; }; f"

Will push the current branch to origin with git pubor another repo with git pub repo-name. Tasty.

将当前分支推送到 origin withgit pub或另一个 repo with git pub repo-name。可口。

回答by Andriy F.

You can push current branch with command

您可以使用命令推送当前分支

git push origin HEAD

(took from here)

(取自这里

回答by kenorb

You can change that default behavior in your .gitconfig, for example:

您可以在您的 中更改该默认行为.gitconfig,例如:

[push]
  default = current

To check the current settings, run:

要检查当前设置,请运行:

git config --global --get push.default

回答by Cody Caughlan

A git push will try and push all local branches to the remote server, this is likely what you do not want. I have a couple of conveniences setup to deal with this:

git push 会尝试将所有本地分支推送到远程服务器,这可能是您不想要的。我有几个方便的设置来处理这个问题:

Alias "gpull" and "gpush" appropriately:

适当地别名“gpull”和“gpush”:

In my ~/.bash_profile

在我的 ~/.bash_profile 中

get_git_branch() {
  echo `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)//'`
}
alias gpull='git pull origin `get_git_branch`'
alias gpush='git push origin `get_git_branch`'

Thus, executing "gpush" or "gpull" will push just my "currently on" branch.

因此,执行“gpush”或“gpull”只会推送我的“currently on”分支。