git - 没有指定分支的 "git push"的默认行为

标签 git branch git-branch git-push

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

git push origin sandbox

如果我说

git push origin

这是否也会将更改推送到我的其他分支,还是只更新我当前的分支?我有三个分支:masterproductionsandbox

git push 文档对此不是很清楚,所以我想澄清一下。

以下 git push 命令准确地更新了哪些分支和远程?

git push 
git push origin
上面的

origin 是一个 Remote 。

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

最佳答案

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

push.default

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

  • nothing:不推送任何内容

  • 匹配:(Git 2.0之前默认)推送所有匹配的分支

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

  • upstream:将当前分支推送到它的upstream分支(tracking是upstream的弃用同义词)

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

  • 简单:(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

命令行示例:

查看当前配置:

git config push.default

设置新配置:

git config push.default current

关于git - 没有指定分支的 "git push"的默认行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/948354/

相关文章:

git - 忽略提交给 git 的文件并将它们从历史记录中删除

git - 如何从 NetBeans IDE 中删除 Git 远程分支?

git - 在 Git 中,列出具有未推送提交的分支名称

Git 在分离的 HEAD 所在的位置创建分支

git 嵌套存储库 - 子模块与符号链接(symbolic link)与其他

git - github 上的多个存储库

git - 是否可以在 git 中的第一次提交时指定分支名称?

git - 通过 Git 远程跟踪分支的给定名称如何找到哪个本地分支跟踪它?

git - git checkout --track origin/branch 和 git checkout -b branch origin/branch 的区别

git - LibGit2Sharp : Checkout Remote Branch