Course contentsShow
← Computer Science
Lesson 2180 of 2,87249. Version Control with GitFree lesson

Amending the Last Commit

Modify the most recent commit with 'git commit --amend' to fix messages or add forgotten files.

Amending the Last Commit

What you'll learn: How to fix mistakes in your most recent Git commit without creating a new commit.

The Problem

You've just made a commit, then immediately notice a typo in the commit message—or realize you forgot to include a file. Creating a brand-new commit for such a tiny fix clutters your history with "oops" commits. Git offers a cleaner solution.

How git commit --amend Works

The --amend flag modifies your most recent commit in place. Think of it like an "undo-redo" for your last save point: Git replaces the previous commit with a new one that incorporates your changes.

Two Common Use Cases

1. Fixing the commit message:

git commit --amend

This opens your editor, letting you rewrite the message. Save and close, and Git replaces the old commit with the corrected version.

2. Adding forgotten files:

git add forgotten_file.txt
git commit --amend

Stage the missing file with git add, then amend. Git rolls the new file into the previous commit as if you'd included it originally. You can keep the same message or change it.

Important Limitation

Only amend commits that haven't been pushed to a shared repository. Amending rewrites history—the new commit has a different hash than the old one. If teammates already pulled your original commit, amending creates conflicts and confusion.

Analogy

Imagine signing a contract in pen, then noticing a typo before handing it in. Amending is like using correction tape and rewriting—the document stays singular and clean. Creating a new commit would be like stapling a "corrections" addendum on top.

Key Takeaway: Use git commit --amend to quietly fix your most recent commit's message or contents—but only before sharing it with others.