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.
A simple way to 'uncommit' your last commit is to use hg strip -r -1 -k. In case the link breaks, the documentation mentioned by @phb states: hg rollback Roll back the last transaction (DANGEROUS) (DEPRECATED) Please use 'hg commit --amend' instead of rollback to correct mistakes in the last commit.
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:
git log
.git reset --hard sha
to return back.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With