Git - 推送当前分支快捷方式

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

Git - push current branch shortcut

git

提问by Elad

Is there a shortcut to tell Git to push the current tracking branch to origin?
Note:I know that I can change the default push behavior, but I am looking for an ad-hoc solution that does not change the default behavior.

是否有快捷方式告诉 Git 将当前跟踪分支推送到原点?
注意:我知道我可以更改默认推送行为,但我正在寻找一种不会更改默认行为的临时解决方案。

For example, suppose I am on branch feature/123-sandbox-testsI would be using

例如,假设我在feature/123-sandbox-tests我将使用的分支上

git push origin feature/123-sandbox-tests

which is tedious. I am looking for a shortcut, something like

这是乏味的。我正在寻找一条捷径,比如

git push origin current

where git knows that current is feature/123-sandbox-tests.

git 知道当前是feature/123-sandbox-tests.



Edit: Starting from version 2.0, git's defaultbehavior has changed to a more intuitive behavior, which is what I wanted to achieve. See This SO questionfor details.

编辑:从 2.0 版本开始,git 的默认行为已更改为更直观的行为,这正是我想要实现的。有关详细信息,请参阅此 SO 问题

Edit 2: ceztko's answeris the best answer as it allows to push the current branch, regardless of the settings.

编辑 2ceztko 的答案是最好的答案,因为它允许推送当前分支,而不管设置如何。

回答by ceztko

According to git pushdocumentation:

根据git push文档:

git push origin HEAD
    A handy way to push the current branch to the same name on the remote.

So I think what you need is git push origin HEAD. Also it can be useful git push -u origin HEADto set upstream tracking information in the local branch, if you haven't already pushed to the origin.

所以我认为你需要的是git push origin HEAD. git push -u origin HEAD如果您尚未推送到源,则在本地分支中设置上游跟踪信息也很有用。

回答by Mahesh

You can configure gitto push to the current branch using the following command

您可以使用以下命令将 git 配置为推送到当前分支

git config --global push.default current

then just do

然后就做

git push 

this will push the code to your current branch.

这会将代码推送到您当前的分支。

回答by Najor

You should take a look to a similar question in Default behavior of "git push" without a branch specified

您应该在没有指定分支的情况下查看“git push”的默认行为中的类似问题

Basically it explains how to set the default behavior to push your current branch just executing git push. Probably what you need is:

基本上它解释了如何设置默认行为以推送您正在执行的当前分支git push。大概你需要的是:

git config --global push.default current

git config --global push.default current

Other options:

其他选项:

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

回答by theodor

I use such alias in my .bashrc config

我在我的 .bashrc 配置中使用这样的别名

alias gpb='git push origin `git rev-parse --abbrev-ref HEAD`'

On the command $gpbit takes the current branch name and pushes it to the origin.

在命令中,$gpb它采用当前分支名称并将其推送到原点。

Here are my other aliases:

这是我的其他别名:

alias gst='git status'
alias gbr='git branch'
alias gca='git commit -am'
alias gco='git checkout'

回答by Ryan Rebo

For what it's worth, the ultimate shortcut:

对于它的价值,终极捷径:

In my .bash_profileI have alias push="git push origin HEAD", so whenever i type pushI know I'm pushing to the current branch I'm on.

在我的.bash_profileI have 中alias push="git push origin HEAD",所以每当我输入时,push我都知道我正在推送到我所在的当前分支。

回答by Faruk Sahin

If you are using git 1.7.x, you can run the following command to set the remote tracking branch.

如果您使用的是 git 1.7.x,您可以运行以下命令来设置远程跟踪分支。

git branch --set-upstream feature/123-sandbox-tests origin/feature/123-sandbox-tests

Then you can simply use git pushto push all the changes. For a more complete answer, please see the accepted answer to a similar question here.

然后您可以简单地使用git push推送所有更改。有关更完整的答案,请在此处查看对类似问题的已接受答案。

If you only want to push the current branch with the push command, then you can change the push behaviour to upstream:

如果您只想使用 push 命令推送当前分支,那么您可以将推送行为更改为上游:

git config --global push.default upstream

回答by Ryan Stewart

The simplest way: run git push -u origin feature/123-sandbox-testsonce. That pushes the branch the way you're used to doing it and also sets the upstream tracking info in your local config. After that, you can just git pushto push tracked branches to their upstream remote(s).

最简单的方法:运行git push -u origin feature/123-sandbox-tests一次。这会按照您习惯的方式推送分支,并在本地配置中设置上游跟踪信息。之后,您可以git push将跟踪的分支推送到其上游远程。

You can also do this in the config yourself by setting branch.<branch name>.mergeto the remote branch name (in your case the same as the local name) and optionally, branch.<branch name>.remoteto the name of the remote you want to push to (defaults to origin). If you look in your config, there's most likely already one of these set for master, so you can follow that example.

您也可以通过设置branch.<branch name>.merge远程分支名称(在您的情况下与本地名称相同)和可选的branch.<branch name>.remote要推送到的远程名称(默认为原点)来自己在配置中执行此操作。如果您查看您的配置,很可能已经为 设置了其中一个master,因此您可以按照该示例进行操作。

Finally, make sure you consider the push.defaultsetting. It defaults to "matching", which can have undesired and unexpected results. Most people I know find "upstream" more intuitive, which pushes only the current branch.

最后,请确保考虑push.default设置。它默认为“匹配”,这可能会产生不希望的和意外的结果。我认识的大多数人发现“上游”更直观,它只推送当前分支。

Details on each of these settings can be found in the git-config man page.

这些设置的详细信息可以在git-config 手册页中找到

On second thought,on re-reading your question, I think you know all this. I think what you're actually looking for doesn't exist. How about a bash function something like (untested):

想一想,在重新阅读你的问题时,我认为你知道这一切。我认为你真正要找的东西不存在。bash 函数如何(未经测试):

function pushCurrent {
  git config push.default upstream
  git push
  git config push.default matching
}

回答by Bantak

With the help of ceztko's answer I wrote this little helper function to make my life easier:

在 ceztko 的回答的帮助下,我编写了这个小辅助函数来让我的生活更轻松:

function gpu()
{
    if git rev-parse --abbrev-ref --symbolic-full-name @{u} > /dev/null 2>&1; then
        git push origin HEAD
    else
        git push -u origin HEAD
    fi
}

It pushes the current branch to origin and also sets the remote tracking branch if it hasn't been setup yet.

它将当前分支推送到原点,如果尚未设置,还会设置远程跟踪分支。