git 推送 -> 仅代码审查当前更改(remote.origin.push 覆盖 push.default)

git push -> code review only current change (remote.origin.push overrides push.default)

我的团队正在使用 gerrit 代码审查,本质上这意味着默认推送行为会绕过标准工作流程,因此我们需要使用 git push origin HEAD:refs/for/feature 来正确推送我们的代码以供审查。

默认推送行为如下所示:

user$ git push --dry-run
To https://gerrit.company.url/project
   83fa2a5..aca3a22  feature -> feature

这会绕过不需要的审核过程。

当我将推送参考规范 (reference here) 设置为 refs/heads/*:refs/for/* 时,这朝着正确的方向迈出了一步:

user $ git config remote.origin.push refs/heads/*:refs/for/*
user$ git push --dry-run
To https://gerrit.company.url/project
 * [new branch]      master -> refs/for/master
 * [new branch]      old_stuff -> refs/for/old_stuff
 * [new branch]      feature -> refs/for/feature

现在它试图将 feature 推到 refs/for/feature,这是我想要的,但它也试图将我所有的分支推到原点。 Gerrit 拒绝了多个请求,所以我得到这样的输出:

user$ git push
....
To https://gerrit.company.url/project
 ! [remote rejected] master -> refs/for/master (no new changes)
 ! [remote rejected] old_stuff -> refs/for/old_stuff (duplicate request)
 ! [remote rejected] feature -> refs/for/feature (duplicate request)

但我发现,如果我命名当前分支,它会如我所料:

user $ git push origin feature --dry-run
To https://gerrit.company.url/project
 * [new branch]      feature -> refs/for/feature

这很好,我将能够使用它,但我想缩小它的范围。我想如果我将 push.default 设置为 current 这将意味着 git push 只会以这种方式推送当前分支,但令我失望的是:

user$ git config push.default current
user$ git push origin --dry-run
To https://gerrit.company.url/project
 * [new branch]      master -> refs/for/master
 * [new branch]      old_stuff -> refs/for/old_stuff
 * [new branch]      feature -> refs/for/feature

这似乎忽略了 push.default 设置,来自 git config documentation:

push.default

Defines the action git push should take if no refspec is explicitly given

所以 remote.origin.push 配置被解释为显式引用规范?即使将默认推送行为设置为 nothing,它仍会尝试推送所有分支:

user$ git config push.default nothing
user$ git push
fatal: You didn't specify any refspecs to push, and push.default is "nothing".
user$ git config remote.origin.push refs/heads/*:refs/for/*
user$ git push origin --dry-run
To https://gerrit.company.url/project
 * [new branch]      master -> refs/for/master
 * [new branch]      old_stuff -> refs/for/old_stuff
 * [new branch]      feature -> refs/for/feature

我在这里错过了什么?如何让 git pushfeature -> refs/for/feature 一样只推送当前分支?

我不相信你想做的事目前只需要 git 就可以完成。

通过在我的配置文件中定义远程服务器别名,我已经能够将它进一步缩小一级,如下所示,一个用于我可能想要推送的每个分支,但这显然是乏味和令人讨厌的,但我仍然必须输入类似 git push masterrev.

的内容
    [remote "origin"]
        url = ssh://gerritserver/product
        fetch = +refs/heads/*:refs/remotes/origin/*
    [remote "masterrev"]
        url = ssh://gerritserver/product
        fetch = +refs/heads/*:refs/remotes/origin/*
        push = HEAD:refs/for/master

据我所知,提供您最初目标的唯一等价物是使用 3rd 方工具,例如 OpenStack 开发人员组合起来供自己使用的 git-review 工具。如果您的团队计划使用 Gerrit,这可能是值得的,因为它提供了其他功能,例如轻松访问精选或检查评论。

顺便说一句,我相信 "current" 的 push.default 设置意味着假设本地分支名称应该通过 remote.*.push 修改,即使它们尚不知道在服务器上存在。它不限制将哪些分支推送到 "current" 一个,而是使用 "current" 名称。