Git: Is there an equivalent to hg strip?

escouten picture escouten · Mar 31, 2012 · Viewed 8.3k times · Source

I have a repo where I made a change that is causing merge hell and I'd like to pretend it never existed. Long, complicated story involving splicing a pre-existing repo on top of one that is updated via git-p4, but the upshot is I really, really want git to pretend a certain change never existed.

If it were Mercurial, I'm pretty sure I could fix my problem with hg strip, but I can't find such a command in Git.

Thanks for any suggestions you might have.

Answer

Eric picture Eric · Apr 29, 2012

There is more than one command that you need to do. The first, as other people have mentioned, is git reset. You'll want to find the changeset just before the one you want to get rid of, and use

git reset --hard <changeset>

This will change the current branch head (and the index) to point at that changeset, but the "bad" changeset is still present. It won't get included if you push, but it will be included if you clone your local repository and it can still be referenced in log and checkout commands.

Assuming there are no other references to it (e.g. subsequent commits, tags, etc...) you can then clean it up with:

git gc --prune=now

I found this command thanks to http://help.github.com/remove-sensitive-data/, which also mentions that (as with hg strip) if you've pushed that "bad" changeset to a remote location you can't remove it with regular git commands but you'll need to take additional steps to remove and recreate the repository on the server and clean up any cached pages.