Git 保留最新的提交但删除之前的提交

Git keep latest commit but delete the one before

我正在尝试将最新的 Git-Commit 推送到 gerrit。 但不知何故,我收到一条错误消息,说我无法推送,因为两个提交具有相同的 Change-ID。

在下图中您可以看到当前的情况。

  1. 第三次提交(从顶部开始)已经推送到 gerrit。
  2. 第三次和第二次提交以某种方式具有相同的 Change-ID。
  3. 我想将第一个提交推送到 gerrit。

.

所以我解决这个问题的想法是删除第二个提交。 这可能吗?

假设三个commit的commit hashes从上到下分别是A,B,C

确保 git status 表明它是干净的。如果没有,先运行git stash

如果你真的不要B了,

git reset C --hard
git cherry-pick A

如果你还想要B,

git reset B --hard
git commit --amend
# Edit the commit message and delete the change-id line, save and exit.
# The commit-msg hook will generate a new changeid.
git cherry-pick A

然后再推。