Rewriting commits with git is easier than you think

Trying to help

One of the things I’ve liked about git is how much it encourages responsible and clean commits. Personally, I’m not there yet. I commit when I think to, usually when I’m at a reasonable stopping point, which means I just end up doing git commit -a rather than adding and committing clean groups of related changes.

Because of this, when I submitted a pull request to the awesome Pandoc, my commit history was a mess. The owner of the project, John MacFarlane, reasonably asked to to separate things into nice clean commits. Embarrassingly, I’d never done this before, but everything went a bit smoother than expected.

The git rebase command allows you to rewrite the commit history of your project. There are lots of ways to do this, but I found the easiest to be:

git rebase -i HEAD~10

Which will allow you to edit the ten most recent commits.

The git documentation describes how to use this, but I had trouble using the squash option and just ended up editing them all.

As mentioned there, git reset HEAD^ resets the staged commit and then you’re free to commit as you wish before using git rebase --continue to move forward.

Comments