-2

How can I obliterate a commit, but keep subsequent commits intact?

In this scenario, suppose I want to get rid of b but keep c.

$ git init
$ for i in a b c; do date >$i; git add $i; git commit -m add-$i;done
$ git log
commit 39c976fa711b38d4c8161af9b6b4a9a91489d079
add-c
commit 2913863926f8c3a3898a2763fe4384b6ca7227a0
add-b
commit ff591ea43a84069e5effd28125b56243dc264336
add-a

doing a reset --hard will get rid of b, but also gets rid of c.

git reset --hard ff591e
HEAD is now at ff591ea add-a

How can I perform this operation while keeping the add-c commit intact?

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

1 Answers1

1

To keep everything in tact (is the sha1 of the commit ) you would do a git revert b where b is the sha1 of b. This adds another commit negating the effect of b

To remove b from history git rebase -i b^

Again where b is the sha1 of the commit

Then delete the line save and close

I would suggest the first way if there is anyone else working on the same repo

exussum
  • 18,275
  • 8
  • 32
  • 65