在gerrit中重命名一个分支

rename a branch in gerrit

我的当前分支名称 "foo" 中的 Gerrit 审查发生了变化。 我想将分支名称更改为 "bar",所以我使用了以下命令。

$ git branch -m bar

然后我进行了 git 修改,因为我需要更改分支名称,这也会出现在 Gerrit 审查中。

$ git commit --amend

然而,在进行 git 审核时,我遇到了以下错误。

$ git review bar
The branch 'bar' does not exist on the given remote 'gerrit'.
If these changes are intended to start a new branch, re-run with the
'-R' option enabled.

如何重命名 Gerrit 审查中的变更分支?

我不想删除并创建另一个分支。

重命名本地分支后,要重命名远程分支,您必须:

  • 删除旧的远程分支并推送新的(重命名的)本地分支。

    git push origin :old new

  • 为新的(重命名的)本地分支重置上游分支。

    切换到分支 运行 git push origin -u new

其中 old 指旧名称分支,new 指更名分支。

由于bar还不存在,您需要先创建它。要创建分支,帐户必须被授予 Create Reference 对引用 refs/heads/* 的访问权限。如果您的帐户没有访问权限,请向您的 Gerrit 管理员寻求帮助。

bar 应该是从当前补丁集的父提交创建的。您可以在当前更改的页面上找到parent

创建 bar 后,您可以在更改页面上将补丁集挑选到 bar 上。或者在本地仓库中进行:

# Checkout or reset to the patchset commit
git checkout <commit>

# Change the commit hash, as the original one has been pushed to refs/for/foo.
# Otherwise, it would fail to push the same commit to refs/for/bar.
git commit --amend --no-edit

# Push the new patchset to bar
git push origin HEAD:refs/for/bar

如果您实际上是想针对新分支名称推送更改以供审核,那么您的本地分支名称完全无关紧要。您推送到的目标分支是相关的。我不确定您将如何使用 git review(因为它 hides/automates 目标分支;例如 HEAD:refs/for/foo)。


您可以做的是将评论移至另一个分支 (ssh command line, see --move or REST API / web interface) - 自 2.13 起可用的功能:


手动推送,直接推送到新的refs/for/...远程分支:

git push origin HEAD:refs/for/bar

这假定您的项目设置允许在同一项目中具有相同的 Change-Id。参见 project setting: receive.createNewChangeForAllNotInTarget。如果您的 Gerrit 远程不接受更改,您可能需要重新创建 Change-Id(删除、修改、提交,您的挂钩应该负责设置一个新的)。然后它将创建一个针对新远程分支的新更改。