npkllr
npkllr

Reputation: 596

Git keep latest commit but delete the one before

I'm trying to push the newest Git-Commit to gerrit. But somehow I get an error message saying that I can't push, because two commits have the same Change-ID.

In the image below you can see the current situation.

  1. The third commit (from the top) is already pushed to gerrit.
  2. The third and the second commit somehow have the same Change-ID.
  3. I want to push the first commit to gerrit.

enter image description here .

So my idea to fix this problem would be to delete the second commit. Is this possible?

Upvotes: 1

Views: 437

Answers (1)

ElpieKay
ElpieKay

Reputation: 30858

Suppose the commit hashes of the three commits from top to bottom are A, B and C.

Make sure git status tells it's clean. If not, run git stash first.

If you really don't want B any more,

git reset C --hard
git cherry-pick A

If you still want 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

And then push again.

Upvotes: 3

Related Questions