简体   繁体   中英

rename a branch in gerrit

I have a change in Gerrit review in my current branch name called "foo". I want to change the branch name to "bar", so I've used following command.

$ git branch -m bar

and then I did the git amend as I need the change of branch name appears to Gerrit review as well.

$ git commit --amend

However while doing git review, I'm getting following error.

$ 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.

How can I rename the branch of change which is in Gerrit review?

I don't want to delete and create an another branch.

Since bar does not exist yet, you need to create it first. To create a branch, the account must be granted the access of Create Reference on the reference refs/heads/* . If your account does not have the access, ask your Gerrit admin for help.

bar is supposed to be created from the parent commit of the current patchset of your change. You can find parent on the page of your current change.

After bar is created, you can cherry-pick the patchset onto bar on the change page. Or do it in the local repository:

# 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

After you rename the local branch, to rename the remote branch, you'll have to:

  • Delete the old remote branch and push your new (renamed) local branch.

    git push origin :old new

  • Reset the upstream branch for the new (renamed) local branch.

    Switch to the branch and run git push origin -u new

Where old refers to the old name branch and new refers to the renamed branch.

If you actually mean to push a change for review targeting a new branch name, your local branch name is totally irrelevant. The target branch you push to is relevant. I'm not sure how you would do that using git review (as it hides/automates the target branch; eg HEAD:refs/for/foo ).


What you could do is moving the review to another branch ( ssh command line, see --move or REST API / web interface ) - a feature available since 2.13:

在此输入图像描述


For pushing manually, just push it to the new refs/for/... remote branch:

git push origin HEAD:refs/for/bar

This assumes that your project settings allow to have the same Change-Id within the same project. See project setting: receive.createNewChangeForAllNotInTarget . If your Gerrit remote does not accept the change, you may want to recreate the Change-Id (remove, amend, commit and your hook should take care of setting a new one). Then it would create a new change targeting the new remote branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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