如何配置 git push 以在没有 -u 的情况下自动设置上游?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17847213/
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
How to configure git push to automatically set upstream without -u?
提问by John
I want git push origin
to automatically set the upstream reference when I push a locally-created branch for the first time.
我想git push origin
在第一次推送本地创建的分支时自动设置上游引用。
I know about git push -u
, but I don't want to have to think about whether or not I've used -u
before or otherwise set an upstream reference. In other words, I want git push
to automatically have the effect of git push -u
on any push of a branch that doesn't already have an upstream.
我知道git push -u
,但我不想考虑我-u
之前是否使用过或以其他方式设置上游引用。换句话说,我想git push
自动对git push -u
没有上游的分支的任何推送产生影响。
Is this possible? If it requires an alias or utility script, that's fine.
这可能吗?如果它需要别名或实用程序脚本,那很好。
回答by Andrea Bergonzo
You can configure it with git config
using git config --global push.default current
.
您可以git config
使用配置它git config --global push.default current
。
Docs: https://git-scm.com/docs/git-config/#Documentation/git-config.txt-pushdefault
文档:https: //git-scm.com/docs/git-config/#Documentation/git-config.txt-pushdefault
回答by mechanicalfish
Since I don't think this is possible using git config, here is what you can do in bash:
由于我认为使用 git config 无法做到这一点,因此您可以在 bash 中执行以下操作:
[[ $(git config "branch.$(git rev-parse --abbrev-ref HEAD).merge") = '' ]] && git push -u || git push
If the current branch has a remote tracking branch, it calls git push
otherwise it calls git push -u
如果当前分支有远程跟踪分支,则调用,git push
否则调用 git push -u
回答by VonC
Note: the fact that the new default push policy "simple
"relies on a branch having an upstream one means that:
setting an upstream branch is viewed as a voluntary step, not an hidden automated one
注意:新的默认推送策略“ simple
”依赖于具有上游分支的分支这一事实意味着:
设置上游分支被视为自愿步骤,而不是隐藏的自动化步骤
When "
git push [$there]
" does not say what to push, we have used the traditional "matching" semantics so far (all your branches were sent to the remote as long as there already are branches of the same name over there).We will use the "
simple
" semantics that pushes the current branch to the branch with the same name, only when the current branch is set to integrate with that remote branch.
There is a user preference configuration variable "push.default
" to change this.
当“
git push [$there]
”没有说明要推送什么时,到目前为止我们一直使用传统的“匹配”语义(只要那里已经有同名的分支,您的所有分支都会发送到远程)。我们将使用 "
simple
" 语义将当前分支推送到具有相同名称的分支,仅当当前分支设置为与该远程分支集成时。
有一个用户首选项配置变量“push.default
”来改变它。
So building up from mechanicalfish's answer, you can define an alias, with the right double quotes ("
) escaped (\"
):
因此,根据机械鱼的回答,您可以定义别名,并使用正确的双引号 ( "
) 转义 ( \"
):
git config alias.pu "![[ $(git config \"branch.$(git rev-parse --abbrev-ref HEAD).merge\") = '' ]] && git push -u || git push"
git pu origin
Sc0ttyDproposes in the commentsthe following alias:
alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'
In multiple lines:
在多行中:
alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] &&
git push -u origin $(git symbolic-ref --short HEAD) ||
git push'
回答by Frexuz
I've had the same problem. I've found this alias (.gitconfig)
我遇到了同样的问题。我找到了这个别名 (.gitconfig)
[alias]
track = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"
[alias]
track = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"
Usage: git track
once per new branch (currently checked out). Then just push as normal :)
用法:git track
每个新分支一次(当前已签出)。然后像往常一样推:)
回答by Mark
The answers by @VonC and @Frexuz are helpful, but both of their solutions produce an error for me. Using both of their answers, I cobbled together something that works for me:
@VonC 和 @Frexuz 的回答很有帮助,但他们的两种解决方案都给我带来了错误。使用他们的两个答案,我拼凑了一些对我有用的东西:
[alias]
pu = ![[ $(git config "branch.$(git symbolic-ref --short HEAD).merge") = '' ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push
This results in executing either git push -u origin $BRANCHNAME
or git push
, depending on whether its upstream (property branch.$BRANCHNAME.merge
) is defined.
这会导致执行git push -u origin $BRANCHNAME
或git push
,具体取决于是否branch.$BRANCHNAME.merge
定义了其上游(属性)。
Entering this alias on the command line will require escape codes, so it's probably easiest to use an editor to insert into the correct file ($HOME/.gitconfig
(global), .git/config
(local), or /etc/gitconfig
(system) )
在命令行输入这个别名需要转义码,所以使用编辑器插入正确的文件($HOME/.gitconfig
(global), .git/config
(local), or /etc/gitconfig
(system) )可能是最简单的
回答by Boris Dalstein
Short answer
简答
If you actually like to be explicit and use the -u
option when necessary,
but just don't want to type the whole:
如果您确实喜欢明确并-u
在必要时使用该选项,但只是不想输入整个内容:
git push -u origin foo
Then you can use the following alias:
然后您可以使用以下别名:
[alias]
push-u = !git push -u origin $(git symbolic-ref --short HEAD)
And simply type:
只需输入:
git push-u
Long answer
长答案
Typically, the need for -u
(shorthand for --set-upstream
) is when we have just created a new local branch and commit, and we want to push it upstream. The remote repository doesn't yet have the new branch, so we need to tell git to create and track the remote branch before pushing the commit. This is only necessary for the first push on the branch. Here is a typical scenario:
通常,需要-u
(shorthand for --set-upstream
) 是当我们刚刚创建了一个新的本地分支并提交时,我们希望将其推送到上游。远程仓库还没有新的分支,所以我们需要在推送提交之前告诉 git 创建和跟踪远程分支。这仅在第一次推送分支时才需要。下面是一个典型的场景:
git checkout -b foo # Create local branch
git commit -m "Foo" # Create local commit
git push -u origin foo # Create and track remote branch, and push commit
git commit -m "Bar" # Create local commit
git push # Push commit
Personally, I do like the need to be explicit with git push -u
when creating the remote branch: it's a pretty significant operation, sharing a whole new branch to the world.
就我个人而言,我确实喜欢git push -u
在创建远程分支时需要明确说明:这是一项非常重要的操作,向全世界共享一个全新的分支。
However, I hate that we have to explicitly write git push -u origin foo
. Not only it is a pain to type, but more importantly, it's quite error-prone! It's easy to make a mistake when typing the branch name, and the new remote branch won't have the same name as your local branch! In most cases, really, you want the upstream repository to be origin
, and the upstream branch to have the same name as your local branch.
但是,我讨厌我们必须明确地编写git push -u origin foo
. 不仅打字很痛苦,而且更重要的是,它很容易出错!输入分支名称很容易出错,新的远程分支不会和你本地分支同名!在大多数情况下,实际上,您希望上游存储库为origin
,并且上游分支与您的本地分支具有相同的名称。
Therefore, I'm using the following alias in my .gitconfig
, which is a subset of the excellent answer provided by Mark:
因此,我在 my 中使用了以下别名.gitconfig
,这是Mark 提供的优秀答案的一个子集:
[alias]
push-u = !git push -u origin $(git symbolic-ref --short HEAD)
Now, we can do the following, which is still explicit, but less error-prone:
现在,我们可以执行以下操作,这仍然是明确的,但不太容易出错:
git checkout -b foo # Create local branch
git commit -m "Foo" # Create local commit
git push-u # Create and track remote branch, and push commit
git commit -m "Bar" # Create local commit
git push # Push commit
回答by JT Jobe
I solved this issue by using this simple Bash script. It won't work on existing branches, but if you create all of your branches with this function, you'll always have your upstream branch set automatically.
我通过使用这个简单的 Bash 脚本解决了这个问题。它不适用于现有分支,但如果您使用此功能创建所有分支,您将始终自动设置上游分支。
function con { git checkout -b && git push --set-upstream origin ; }
The $1 represents the first argument you pass after con
so it's just like doing:
$1 代表您之后传递的第一个参数,con
因此就像这样做:
git checkout -b my-new-branch && git push -u my-new-branch
...by just doing this:
...通过这样做:
con my-new-branch
回答by djanowski
Simply:
简单地:
$ alias gush="git push -u origin HEAD"
回答by jvenezia
I made a git extension with useful scripts, including this one:
我用有用的脚本制作了一个 git 扩展,包括这个:
usage: git line push
Push the current branch and set an upstream if needed.
回答by gazdagergo
If you wanna use the built-in git features only with the less possible keys, just type:
如果您只想使用较少可能的键使用内置的 git 功能,只需键入:
$ git push -u o
tabH
tab
$ git push -u o
tabH
tab
and the autocomplete will give you
$ git push -u origin HEAD
和自动完成会给你
$ git push -u origin HEAD