Git: Is there an equivalent to hg strip? Git: Is there an equivalent to hg strip? git git

Git: Is there an equivalent to hg strip?


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.


As larsks said, git reset --hard can help you return to the history you wanted:

  1. Find the correct sha value you want to return to using git log.
  2. Run git reset --hard sha to return back.


Try reading the documentation for git reset. I'm not all that familiar with Mercurial, but if I understand this document correctly, git reset will let you do the same thing -- that is, reset your repository back to a previous point in it's history.

This document discusses the reset command in some detail, and this one briefly discusses different options for correcting mistakes.