Skip to main content

Fixing your last Git commit

Posted in Development and Git

Since I’ve started using Git on the command line, there’s one ‘new’ thing that I’ve used more than any other: --amend. I can now amend the most recent commit on the branch I’m on. So far, I’ve used it for two things:

  1. Fixing typos in a commit message
  2. Adding missed files into a commit

First up, a big thank you to my friend Sam Beckham for putting me on to --amend – it has been invaluable.

Typos

Like most humans, I make typos. Most of time I know when I’ve made a spelling mistake in a commit message and fix the mistake, but every now and then I don’t notice until I’ve hit .

A simple git commit --amend opens up the last commit in my editor, allowing me to fix those typos or even change the whole commit message if I wasn’t happy with it before.

Adding missed files

This one’s pretty common too – I’ll have been working on a bunch of files but only select few make sense as a single commit. I’ll stage (add) them, commit, then realise I missed one. Luckily, it’s possible to add a file (or several files) to that last commit:

  1. First, add the file(s):
    • For all remaining unstaged files, use git add .
    • If only one file has to be added in, git add path/to/file.html
    • To add multiple files, run git add path/to/file1.html path/to/file2.html, adding as many files as needed
  2. Amend the commit with the files you’ve added: git commit --amend

You then get the chance to amend your commit message, but you can just save the commit and it’s done.

If you’re having a particularly bad day and make a new typo while you’re fixing that first one, or you add one new file and realise there’s a second one too, don’t fret! You can repeat the process as many times as you need to!

Removing files

“But what about removing files from a commit!?” I hear you ask. I’ve only needed to do this once or twice and it’s a wee bit different (and trickier) than using --amend.

  1. First, undo the commit you just made with git reset --soft HEAD~1, which brings all the files from your last commit back to staging
  2. Next, remove the files you didn’t want in that commit from the staging area: git reset HEAD path/to/file.html, or if there are several, use git reset HEAD path/to/file1.html path/to/file2.html
  3. Now you want to make that commit again; this time without those files. Run git commit -c ORIG_HEAD which creates a new commit using the same (amendable) commit message that you undid in step 1

As for those files you took out of the commit, feel free to do whatever you want with them!

Accessibility in your inbox

I send an accessibility-centric newsletter on the last day of every month, containing:

  • A roundup of the articles I’ve posted
  • A hot pick from my archives
  • Some interesting posts from around the web

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that’s not enough, have a look at the full list.

  1. Upgrading from iPhone 13 mini to 16 Pro

    I get a new phone every 3-ish years, give mine to my wife, and now she gives hers to our daughter. I got a 16 Pro this year! Here’s the skinny.

  2. Apple, you’re doing the Dynamic Island wrong

    I love the idea of Dynamic Island; making lemons into lemonade and all that. But, in my opinion, Apple have got the fundamentals mixed up.