简体   繁体   English

在推送新分支后自动设置上游

[英]Automatically set upstream after pushing a new branch

Here is the thing: 这是事情:

$ git checkout -b new-branch
Switched to a new branch 'new-branch'

$ git push
...
 * [new branch]      new-branch -> new-branch

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to rebase against.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> new-branch

I've seen other questions, and the answers suggest doing git push -u when doing the first push. 我已经看到了其他问题,答案建议在第一次推送时执行git push -u But I always forget doing this. 但我总是忘记这样做。

Is there a configuration option to automatically set the upstream tracking branch when a new branch is pushed from my repo to the remote repo? 是否有一个配置选项可以在将新分支从我的仓库推送到远程仓库时自动设置上游跟踪分支?

You can achieve something you can live with by creating an alias that would automatically use the -u switch. 您可以通过创建一个自动使用-u开关的别名来实现您可以使用的功能。 For example: 例如:

git config --global alias.pu "push -u"

And then use it like 然后像使用它一样

git pu

Unfortunately, you can't override any original git command with a git alias. 不幸的是,你不能用git别名覆盖任何原始的 git命令。 You can try to shadow git push command with a bash alias and add the -u switch by default this way. 您可以尝试使用bash别名影响git push命令,并以这种方式默认添加-u开关。

Is there a configuration option to [imply -u when the branch gets created by git push ]? 有没有配置选项[暗示-u当分支由git push创建时]?

There probably should be, but there isn't. 可能应该有,但没有。 You can, however, fix things up afterward using git branch --set-upstream-to , just as in the Git error message. 但是,您可以使用git branch --set-upstream-to ,就像在Git错误消息中一样。

As in the other answers, you can set up some alias(es). 与其他答案一样,您可以设置一些别名。 To expand a bit on fracz's answer , it is safe use the -u option every time: this will set the upstream on every push, but assuming you are always pushing to the same place , you will "change" the upstream from origin/ branch to origin/ branch each time, ie, not actually change anything. 为了扩展fracz的答案 ,每次都安全使用-u选项:这将在每次推送时设置上游,但假设你总是推到同一个地方 ,你将从origin/ branch “改变”上游每次都origin/ branch ,即实际上没有改变任何东西。

Except for the progress output, there's not even a way to tell whether git push actually created the branch on the remote. 除了进度输出之外,甚至没有办法判断git push是否在遥控器上实际创建了分支。 (The push command itself can tell, but it only reports this as that message, [new branch] or not [new branch] .) You can get fairly close by running git ls-remote before the push, but there is a race here: if ls-remote reports that the branch did not exist, and the push succeeds, you don't know for sure that your push created it, because there's a very slight possibility that someone else created it, but your push succeeded anyway. push命令本身可以告诉,但它只报告这个消息, [new branch]或不[new branch] 。)你可以通过在推送前运行git ls-remote来获得相当接近,但这里有一场比赛:如果ls-remote报告分支不存在,并且推送成功,则您不确定您的推送是否创建了它,因为其他人创建它的可能性非常小,但无论如何您的推送都成功了。

It is, however, easy to tell whether there is an upstream before your git push started. 但是,在git push开始之前,很容易判断是否存在上游。

Here's a (somewhat imperfect and totally untested) script that does what I think git push should probably actually do. 这是一个(有些不完美且完全未经测试)的脚本,它可以完成我认为git push应该实际执行的操作。

#! /bin/sh
#
# Push, and set upstream if the push succeeds and
# there was no upstream set before the push started,
# based on "push.set-upstream" setting

die() {
    printf "%s\n" "$1" 1>&2
    exit 1
}

get_current_branch() {
    git symbolic-ref -q --short HEAD ||
        die "fatal: not currently on a branch"
}

get_remote() {
    git config --get branch."$1".remote || echo origin
}

upstream_is_set() {
    git rev-parse -q --verify "$1@{u}" >/dev/null 2>&1
}

# arguments are: [remote [branch]]
# we do not accept arbitrary refspecs here!

case $# in
0)  branch=$(get_current_branch) || exit $?
    remote=$(get_remote "$branch")
    ;;
1)  remote="$1"
    branch=$(get_current_branch) || exit $?
    ;;
2)  remote="$1" branch="$2";; # should check $2 ref format?
*)  die "usage: gitpush [remote [branch]]";;
esac

how_to_set=$(git config --get push.set-upstream || echo auto)

case "$how_to_set" in
always|true) do_set=true;;
false) do_set=false;;
auto|only-if-unset) if upstream-is-set "$branch"; then
        do_set=false
    else
        do_set=true
    fi;;
*) die "fatal: don't understand push.set-upstream=$how_to_set";;
esac

# figured everything out, so now push; fail if push fails
git push "$remote" "$branch" || exit $?

# last, set upstream if still needed
if $do_set; then git branch --set-upstream-to "$remote/$branch"; fi

Use git push -u origin <branch> as your first push. 第一次使用git push -u origin <branch> this will set up tracking. 这将设置跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM