November 15, 2011

Git: Removing Individual Commits With Rebase

I was pretty much scared completely shitless of git rebase for the longest time since it’s really powerful and in the wrong hands (mine) could cause some serious damage.

BUT. It’s actually not that hard to remove a single commit from your history using interactive rebase. I made a couple of embarrassing commits to a public repository that no one’s really looking at yet and figured I’d see if I could just, you know, kind of make it like they never happened in the first place.

To start an interactive rebase session:

git rebase -i HEAD~X

Where X is the number of commits before HEAD that you want to go back and play with (the -i flag is for “interactive”). Git will helpfully and magically open a file in your text editor that contains a list of those commits.

Now delete the offending errant commits from this file. Be a little judicious and think about the possible effects of your actions because you’ll have to resolve any conflicts that arise as a result of removing a commit from your history.

When you save the file and close it, Git will give you a chance to resolve those conflicts. And when you’re done, it’s like those commits were never there.

I’m not just only partially┬áscared shitless of git rebase now, but I’m looking forward to learning what other kind of damage I can do with this feature.