首页 > 解决方案 > 更改推送建议的默认遥控器

问题描述

当我创建一个新分支并第一次执行git push时,它失败并给了我这样的建议:

fatal: The current branch new-branch has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin new-branch

如何更改origin它建议推送到的默认遥控器?我希望它表明我添加的不同来源:git push --set-upstream different-origin new-branch.

标签: gitpushbranch

解决方案


这里打印的名字是通过函数获取的pushremote_for_branch,内容如下:

    if (branch && branch->pushremote_name) {
            if (explicit)
                    *explicit = 1;
            return branch->pushremote_name;
    }
    if (pushremote_name) {
            if (explicit)
                    *explicit = 1;
            return pushremote_name;
    }
    return remote_for_branch(branch, explicit);

此处,如果已设置,则branch->pushremote_name来自配置项。同时,将答案填入。如果这些都没有提供答案,则回退是使用.branch.name.pushRemoteremote.pushDefaultremote_for_branchbranch.name.remote

因此:

git branch newbranch
git config branch.newbranch.pushremote newremote

将保证默认为newremote. 但是,如果您不想运行git config branch.newbranch.pushremote——当然,如果你刚刚创建它,它就不会被配置——那么:

git config remote.pushdefault newremote

将为每个还没有明确的 pushremote 设置的分支设置推荐和默认值。如果这全局化,您将需要一个明确的,无论是还是,以便该设置仅适用于该一个分支。git config branch.newbranch.somethingsomethingpushremoteremote

(你可以拼写它pushRemote,就像文档一样,或者甚至PUSHREMOTE或者PUshreMOte其他,如果你喜欢的话。Git 在内部将所有内容都转换为小写,然后比较小写名称。)


推荐阅读