Reputation: 2973
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.
Upvotes: 29
Views: 9241
Reputation: 2066
As larsks said, git reset --hard
can help you return to the history you wanted:
git log
.git reset --hard sha
to return back.Upvotes: 4
Reputation: 5365
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.
Upvotes: 36
Reputation: 312620
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.
Upvotes: 2