Course contents

Fix the last commit message

Sign in to run this lab and save your progress

Learn

Even the best of us commit with a typo in the message from time to time. Fortunately, fixing the most recent commit's message is almost trivial. This lesson shows the command, and introduces the broader idea of rewriting history, which is what the rest of this module is about.

The command

git commit --amend -m "Add file.txt"

The --amend flag tells Git: "instead of creating a new commit, replace the most recent one with a new version." The -m "..." provides the new message. Everything else about the commit (the files, the changes) stays the same.

If you leave off -m, Git opens your default editor with the existing message pre-filled, and you edit it there. Either way works.

The commit is technically a new commit

Even though it feels like you just edited the existing commit, Git doesn't actually work that way. Commits in Git are immutable once created. What --amend really does is:

  1. Take the changes and metadata of the current commit.
  2. Combine them with whatever you're changing (message, staged files).
  3. Create a brand new commit with that combined content.
  4. Move the branch pointer to point at the new commit instead of the old one.

The old commit still exists in Git's storage for a while (recoverable via git reflog, which is coming up in this module), but from your branch's perspective, it's been replaced. The new commit has a different SHA hash than the old one.

That's what "rewriting history" means. You're not editing history; you're replacing part of it with a new version and pretending the old version never happened.

Why the "different hash" matters

Because the new commit has a different hash, anyone else who had your old commit now has a stranded commit that isn't on your branch anymore. Their Git will get confused when they next pull.

That's why the rule for --amend, and for every other command in this module, is the same:

Never amend, rebase, reset, or otherwise rewrite commits that other people have already pulled.

Your own local commits before you push them: safe. Feature branches only you touch: safe. Anything shared: dangerous.

The reason this matters more here than in day-to-day Git is that you WILL be tempted. Fixing typos and cleaning up before merging is satisfying and the tools are easy. The tools are also irrevocable if you push them onto a shared branch. Keep the rule in mind as you learn the rest of this module.

Your task

The repository has one commit, but the message has a typo: Add fle.txt should be Add file.txt. Fix the message without creating a new commit — there should still be exactly one commit after you're done.