You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅向Gerrit推送HEAD提交,排除已cherry-pick的提交?

问题描述

我cherry-pick了若干变更,希望仅更新并推送顶层提交,但所有已cherry-pick的提交也被更新。例如变更444683生成了新补丁集,提示:

Uploaded patch set 5: New patch set was added with same tree, parent tree, and commit message as Patch Set 4.

我不想为之前cherry-pick的提交创建新补丁集,请问是否有办法避免这种情况?

附上操作过程:

$ git fetch ssh://review-project.myinc.com:29418/myprj/myprj-project refs/changes/83/444683/4 && git cherry-pick FETCH_HEAD
From ssh://git-project-lv.myinc.com:29418/myprj/myprj-project
 * branch                      refs/changes/83/444683/4 -> FETCH_HEAD
[main 2b6e920a0218] Test failure case 3
 Date: Fri Nov 8 06:17:37 2024 -0800
 1 file changed, 3 insertions(+)

$ git fetch ssh://review-project.myinc.com:29418/myprj/myprj-project refs/changes/40/449340/1 && git cherry-pick FETCH_HEAD
remote: Counting objects: 8958, done
remote: Finding sources: 100% (6371/6371)
remote: Total 6371 (delta 3429), reused 5218 (delta 3429)
Receiving objects: 100% (6371/6371), 18.74 MiB | 9.41 MiB/s, done.
Resolving deltas: 100% (3429/3429), completed with 750 local objects.
From ssh://git-project-lv.myinc.com:29418/myprj/myprj-project
 * branch                      refs/changes/40/449340/1 -> FETCH_HEAD
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 74726ac5a732.


$ git commit --allow-empty #-----------> 我更新了提交信息
[main 451839fba790] cherry-pick test for CUI
 Date: Mon Nov 18 10:09:47 2024 -0800

$ git log -1
commit 451839fba790e57f8255c96f6b626ccde3c98ce6 (HEAD -> main)
Author: Neth S <neth@myinc.com>
Date:   Mon Nov 18 10:09:47 2024 -0800

    cherry-pick test

    Testing purpose

    Change-Id: I9c0fc86423ada538ad12072f2e6bb530f6dde258

$ git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/for/main
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 641.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, updated: 2, done
remote: warning: no changes between prior commit 526f331 and new commit 2b6e920
remote:
remote: SUCCESS
remote:
remote:   https://review-project.myinc.com/c/myprj/myprj-project/+/444683 Test failure case 3
remote:   https://review-project.myinc.com/c/myprj/myprj-project/+/449340 cherry-pick test
remote:
To ssh://review-project.myinc.com:29418/myprj/myprj-project
 * [new branch]                451839fba790e57f8255c96f6b626ccde3c98ce6 -> refs/for/main
解决方案

问题核心是推送整个本地提交链时,Gerrit会根据每个提交的Change-Id识别并更新对应变更,哪怕内容无变化。要只推送顶层更新提交,可采用以下方法:

方法1:定向推送目标提交到对应变更路径

直接将更新后的顶层提交推送到目标变更的Gerrit评审分支,避免触发其他变更的补丁集更新:

  1. 确认目标变更编号(比如例子中的449340)
  2. 执行推送命令,指定仅推送该提交:
# 方式1:用变更编号指定
git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/for/main%change=449340

# 方式2:直接用变更的refs路径(补丁集编号可省略,Gerrit自动递增)
git push origin 451839fba790e57f8255c96f6b626ccde3c98ce6:refs/changes/40/449340/2

方法2:重置分支分离不需要的提交

通过软重置将本地分支回退到顶层提交的父节点,仅保留目标提交进行推送:

  1. 查看顶层提交的父提交哈希(例子中451839f的父提交是2b6e920)
  2. 执行软重置,保留顶层提交的修改:
git reset --soft 2b6e920a0218
  1. 重新提交(若需更新提交信息):
git commit -m "cherry-pick test" -m "Testing purpose" --allow-empty
  1. 推送新提交到目标变更:
git push origin HEAD:refs/for/main%change=449340

方法3:全局配置推送默认行为

若经常遇到此问题,可配置Git默认仅推送当前HEAD提交:

git config --global push.default current

之后推送只需执行:

git push origin refs/for/main%change=449340

内容的提问来源于stack exchange,提问作者rodee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 03:50:10