Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Is there an equivalent to hg strip?

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.

like image 315
escouten Avatar asked Mar 31 '12 00:03

escouten


People also ask

How do you Uncommit in Heartgold?

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.


2 Answers

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.

like image 193
Eric Avatar answered Sep 21 '22 16:09

Eric


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.
like image 40
Tim Avatar answered Sep 24 '22 16:09

Tim