How to Change Your Last Git Commit Message

It’s late and you’ve just wrapped up work on a feature branch. You stage your modified files and commit them. And then, just as you’re about to push to Github, you read your last commit and… oops. Maybe it’s just a typo and you’re a perfectionist, or maybe you just inadvertently transcribed a line from the Homeland episode you’re watching. Either way, you want to change that last commit. Sound familiar?

Enter git commit -amend. This little guy has saved me from countless stupid commits. For example, I was watching Homeland when I made this commit (don’t do that) and I got a little carried away:

$ git commit -m "Refactors code; Sgt Brody = sleeper agent"
[master b8619d2] Refactors code; Sgt Brody = sleeper agent
3 files changed, 26 insertions(+), 27 deletions(-)

I can edit that last commit using git commit --amend -m "<new commit goes here>". Let’s clean it up a bit:

$ git commit --amend -m "Refactors code."
[redesign cf5d27f] Refactors code.
3 files changed, 26 insertions(+), 27 deletions(-)

And there you have it. Now the commit looks professional, and my suspicions regarding Sergeant Brody remain under wraps.

comments powered by Disqus