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

发布于 2024-07-22 21:41:38 字数 468 浏览 12 评论 0原文

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

git push origin sandbox

如果我说

git push origin

这也会推送我其他分支中的更改,还是只更新我当前的分支? 我有三个分支:master生产sandbox

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

以下 git push 命令具体更新哪些分支和遥控器?

git push 
git push origin

上面的origin是一个远程。

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

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, production and sandbox.

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

Which branches and remotes do the following git push commands update exactly?

git push 
git push origin

origin above is a remote.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

戈亓 2024-07-29 21:41:40

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

[push]
  default = current

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

git config --global --get push.default

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

[push]
  default = current

To check the current settings, run:

git config --global --get push.default
月棠 2024-07-29 21:41:40

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

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

在我的〜/.bash_profile中

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

因此,执行“gpush”或“gpull”将只推送我的“当前打开”分支。

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:

Alias "gpull" and "gpush" appropriately:

In my ~/.bash_profile

get_git_branch() {
  echo `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
}
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.

一个人的旅程 2024-07-29 21:41:40

我更喜欢创建 git-XXX 脚本,而不是使用别名,这样我就可以更轻松地对它们进行源代码控制(我们的开发人员在此类事情的路径上都有一个特定的源代码控制目录)。

此脚本(称为 git-setpush )会将 remote.origin.push 值的配置值设置为仅推送当前分支的值:

#!/bin/bash -eu

CURRENT_BRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
NEW_PUSH_REF=HEAD:refs/for/$CURRENT_BRANCH

echo "setting remote.origin.push to $NEW_PUSH_REF"
git config remote.origin.push $NEW_PUSH_REF

请注意,正如我们所见使用 Gerrit,它将目标设置为 refs/ for/XXX 推入审核分支。 它还假设 origin 是您的远程名称。

在使用它签出分支后调用它

git checkout your-branch
git setpush

显然可以适应也进行签出,但我喜欢脚本 做一件事并做好

Rather than using aliases, I prefer creating git-XXX scripts so I can source control them more easily (our devs all have a certain source controlled dir on their path for this type of thing).

This script (called git-setpush) will set the config value for remote.origin.push value to something that will only push the current branch:

#!/bin/bash -eu

CURRENT_BRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
NEW_PUSH_REF=HEAD:refs/for/$CURRENT_BRANCH

echo "setting remote.origin.push to $NEW_PUSH_REF"
git config remote.origin.push $NEW_PUSH_REF

note, as we're using Gerrit, it sets the target to refs/for/XXX to push into a review branch. It also assumes origin is your remote name.

Invoke it after checking out a branch with

git checkout your-branch
git setpush

It could obviously be adapted to also do the checkout, but I like scripts to do one thing and do it well

扮仙女 2024-07-29 21:41:40

git 中的新配置 2.37.0

运行以设置自动设置远程,而不是更改

git config --global --add --bool push.autoSetupRemote true

它与推送配合良好的推送默认行为。默认是简单上游

参考:答案
推文
文档
提交

New config in git 2.37.0

Run to set auto setup remote instead of changing the push default behavior

git config --global --add --bool push.autoSetupRemote true

it works well with push.default is to simple, upstream

References: answer
tweet
docs
commit

小伙你站住 2024-07-29 21:41:40

我已将以下函数添加到 .bashrc 文件中以自动执行这些任务。 它执行 git push/git pull + 当前分支的名称。

function gpush()
{
  if [[ "x$1" == "x-h" ]]; then
    cat <<EOF
Usage: gpush
git: for current branch: push changes to remote branch;
EOF
  else
    set -x
    local bname=`git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed -e "s#/# #"`
    git push ${bname}
    set +x
  fi
}

function gpull()
{
  if [[ "x$1" == "x-h" ]]; then
    cat <<EOF
Usage: gpull
git: for current branch: pull changes from
EOF
  else
    set -x
    local bname=`git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed -e "s#/# #"`
    git pull ${bname}
    set +x
  fi
}

I have added the following functions into my .bashrc file to automate these tasks. It does git push/git pull + name of current branch.

function gpush()
{
  if [[ "x$1" == "x-h" ]]; then
    cat <<EOF
Usage: gpush
git: for current branch: push changes to remote branch;
EOF
  else
    set -x
    local bname=`git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed -e "s#/# #"`
    git push ${bname}
    set +x
  fi
}

function gpull()
{
  if [[ "x$1" == "x-h" ]]; then
    cat <<EOF
Usage: gpull
git: for current branch: pull changes from
EOF
  else
    set -x
    local bname=`git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed -e "s#/# #"`
    git pull ${bname}
    set +x
  fi
}
つ可否回来 2024-07-29 21:41:39

我只是将其放入我的 .gitconfig 别名部分,并且喜欢它的工作原理:

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

将使用 git pub 或另一个使用 git pub repo-name 的存储库将当前分支推送到原点。 可口。

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

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

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

熊抱啵儿 2024-07-29 21:41:38

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

push.default

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

  • nothing:不推送任何内容

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

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

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

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

  • simple:(Git 1.7.11 中新增,自 Git 2.0 起默认)与上游类似,但拒绝如果上游分支名称与本地分支名称不同,则推送

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

简单、当前和上游模式适合那些想要在完成工作后推出单个分支的人,即使其他分支尚未准备好推出

命令行示例:

查看当前配置:

git config push.default

设置新配置:

git config push.default current

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

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:

  • nothing: do not push anything

  • matching: (default before Git 2.0) push all matching branches

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

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

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

  • simple: (new in Git 1.7.11, default since Git 2.0) 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.

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

Command line examples:

To view the current configuration:

git config push.default

To set a new configuration:

git config push.default current
征棹 2024-07-29 21:41:38

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

git config push.default current

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

git config --global push.default current

此设置中的当前意味着默认情况下您将仅推送当前分支 当你执行 git Push

其他选项有:

  • Nothing :不推送任何
  • 匹配的内容 :推送所有匹配的分支(默认)
  • 跟踪 :将当前分支推送到它正在跟踪的任何分支
  • current :推送当前分支

更新 - 新方法

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

git config --global push.default simple

这是引入的新设置,其工作方式与当前分支相同,并将默认为 git据传言,从 v 2.0 开始

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

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 current in this setup means that by default you will only push the current branch when you do 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

UPDATE - NEW WAY TO DO THIS

As of Git 1.7.11 do the following:

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

仲春光 2024-07-29 21:41:38

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

git push origin HEAD

(取自此处

You can push current branch with command

git push origin HEAD

(took from here)

跨年 2024-07-29 21:41:38

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

请参阅“请讨论:当你不说要推送什么时,“git Push”应该做什么?"

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

建议将默认值更改为“上游,即仅推送当前分支,并将其推送到 git pull 从中拉取的分支。
另一个候选者是“current”; 这仅将当前分支推送到同名的远程分支。

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

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

之前的相关讨论包括:

要加入讨论,请将消息发送至:[电子邮件受保护]

(March 2012)
Beware: that default "matching" policy might change soon
(sometimes after 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 push without 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:

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]

怪我入戏太深 2024-07-29 21:41:38

以下是关于 Git Push 的非常方便且有用的信息:
Git Push:只是提示

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

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

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

git push origin master:staging 会将本地 master 分支的更改推送到远程 staging 分支(如果存在)。

Here is a very handy and helpful information about 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 will push changes from all local branches to matching branches the origin remote.

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

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

清泪尽 2024-07-29 21:41:38

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

类似于 git push,其中 是当前分支的远程(或源,如果没有为当前分支配置远程) 。

来自 git-push< 的示例部分/code> 手册页

git push origin will push all changes on the local branches that have matching remote branches at origin As for git 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).

From the Examples section of the git-push man page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文