Course contentsShow
← Software Engineering
Lesson 67 of 1,9332. Version Control with GitFree lesson

Amending Commits

Learn when and how to use git commit --amend to fix the most recent commit's message or content before pushing.

Amending Commits

You'll learn when and how to fix your most recent commit before sharing it with others.

Sometimes right after you commit, you realize you made a typo in the commit message or forgot to include a small change in the same logical unit of work. Instead of creating a whole new commit to fix a tiny mistake, Git lets you amend (modify) the most recent commit.

Why Amend?

Think of a commit like sealing an envelope to mail. If you notice a mistake before you drop it in the mailbox, you can open it back up, fix the contents, and reseal it—no one needs to know about your mistake. But once it's mailed (pushed to a remote repository), you shouldn't tamper with it because others might already have it.

Amending keeps your history clean and meaningful. One logical change = one commit.

How to Amend

After making your last commit, if you need to fix it:

  1. To change just the message:

    git commit --amend
    

    This opens your editor to rewrite the commit message.

  2. To add forgotten changes:

    git add forgotten-file.txt
    git commit --amend
    

    This adds the staged changes to the previous commit.

The amended commit replaces the old one entirely—it gets a new commit ID because the content or message changed.

Critical Rule

Only amend commits that haven't been pushed. Once you've shared a commit with others (via push), amending it creates confusion because you're rewriting history that others might have based their work on.

Key Takeaway: Use git commit --amend to fix your most recent commit's message or add forgotten changes, but only before pushing to keep shared history stable.