1 Apr 2012

Rolling back git commits without losing history

I sometimes introduce a buggy feature to a program and I want to roll it back.  If I had already committed the buggy feature into my git repository, I prefer to keep the buggy commit in revision history... just for posterity ;-)

Finding how to do this was a bit hard, so I’ll document it here.  I know I’ll need to look this up another time; it’s easier if it’s on my own blog :)  The command to roll back to a previous commit is:
git checkout <commit> <repository root>
For example, running git checkout ea2ff0a50 ~/d/prog would restore all files under ~/d/prog directory to the state they were at ea2ff0a50 commit.  Now I can commit the “new” rolled back state with a different commit message, for example “Roll back free penguins feature.  Penguins are too cute to be given away.”

The other approach for rolling back, where you lose the buggy commit for good is to use git reset.

2 comments: