Bulwersator
Bulwersator

Reputation: 1132

Remove from history unsynced git commit that is not last one

Currently the best solution that I found is

git reset --soft HEAD^2 

to keep wanted changes

git reset --mixed HEAD^

to kill unwanted commit

unfortunately it will force me to recreate commits made after one that I want to disappear

Upvotes: 1

Views: 2371

Answers (1)

John Ledbetter
John Ledbetter

Reputation: 14173

try git rebase -i <target commit>^

Then when presented with the list of commits to rebase, delete the line for the commit you want to remove.

For example, if I have:

6e99176 most recent commit
60dd2f0 older commit
4c82808 even older commit
b980abe commit i want to remove
5b7254b oldest commit

I can do

git rebase -i b980abe^

And will be presented with:

pick 6e99176 most recent commit
pick 60dd2f0 older commit
pick 4c82808 even older commit
pick b980abe commit i want to remove

# Rebase 50ce3f5..9ad75f8 onto 50ce3f5
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

If you follow the instructions and remove the offending commit, then save and close the file, you will have removed that commit from your git history.

Upvotes: 5

Related Questions