Git tip: create an alias to amend your last commit

This week we have another Git alias that makes it quick and easy to make adjustments to your most recent commit:

  $ git config --global alias.amend 'commit --amend --no-edit'

Now running git amend will take any changes you have staged with git add and amend the previous commit to include them, all in a single command:

  $ git amend
  [feature-x b01ae4c] Fix patient ordering bug
   Date: Fri Jan 17 13:09:19 2020 +0000
   1 file changed, 50 insertions(+), 2 deletions(-)

When is this alias useful?

Imagine you’ve just carefully crafted a new commit only to notice you’ve made a typo in the changes 🤦!

Now you could fix the typo, stage the change and create a brand new commit with a message like “Fixed typo”. But that would leave a useless commit cluttering up your history. Instead of creating a new commit, we can use our new shortcut to amend the commit we just created and wipe the typo from existence.

And it’s not only useful for correcting typos. I often find myself wanting to make adjustments to the code I’ve just written, and it sometimes makes more sense to wrap those adjustments into the original commit instead of creating a brand new one.

Why does this matter?

I’m going to write a bit more about this in a future post, but in short, creating a history that is made up of small, focused, atomic commits has many benefits both for you as you work through changes on a feature, but also for the future maintainability of the software you write.

Breaking down the shortcut

This shortcut is making use of two flags for the git commit command:

The first is --amend. This flag tells Git to attach any staged changes to the existing commit, rather than creating a new one(1). If you call git commit --amend, Git will add the changes and open your editor pre-filled with the previous commit’s message, giving you the opportunity to also amend the message.

The second flag, --no-edit, tells Git you’re happy with the existing commit message so it should go ahead and re-use it.

Thus combining the two flags allows us to: amend the last commit; reuse the existing message; and do so without having to jump to an editor window.

When is this alias not useful?

Extra care should be taken when rewriting history in this way. If the commit has already been published elsewhere, perhaps by pushing it a remote host, you’ll want to make sure amending it isn’t going to cause problems for others. For example if someone has already based their work on the old commit, amending it can cause issues that are difficult to recover from. A simple way to keep yourself out of trouble is to only amend commits on your local feature branches, and if you’ve pushed the commit up already, make sure no one else is using that code before making any amendments.

Do you have a favourite Git alias?

Over the comming weeks I’ll be sharing more of my favourite Git aliases as well as other tips and techniques for working effectively with Git. If you have a Git alias or technique that you’d like to share, drop me a tweet!

1. So technically Git is actually creating a brand new commit, it just happens that it also replaces the old commit with this new one.

Want more juicy Git tips like this straight to your inbox?

You'll get an email whenever I have a fresh insight or tip to share. Zero spam, and you can unsubscribe whenever you like with a single click.

More articles on Git

Authored by Published by