← Back to Git Course | Chapter 5: Undoing Changes | Lesson 4 of 6

Git amend Commit

Changing the Last Commit Message

git commit --amend replaces your most recent commit with a new one, letting you fix a typo in the commit message without cluttering history with a separate "fix typo" commit. It's the cleanest way to correct a mistake you notice seconds after committing, as long as you haven't pushed yet.

Example: Changing the Last Commit Message

bash
git commit --amend -m "Fix typo in README"

Adding Forgotten Files

If you forgot to git add a file before committing, stage it now and run git commit --amend --no-edit to fold it into the previous commit rather than creating a whole new one for a single forgotten file. This keeps your history looking like the forgotten file was there all along.

Example: Adding Forgotten Files

bash
git add forgotten-file.txt
git commit --amend --no-edit

Amending Author Information

Committed under the wrong name or email? git commit --amend --author="Name <email>" rewrites just the author metadata on the last commit, useful when a machine's default Git config doesn't match the identity you meant to commit as.

Example: Amending Author Information

bash
git commit --amend --author="Priya Sharma <[email protected]>"

Amending After Pushing

Amending a commit changes its hash, so if you already pushed the original, your local and remote histories now disagree — a plain git push will be rejected. You'll need git push --force-with-lease to overwrite the remote branch, and anyone else who already pulled the old commit will need to reconcile their copy.

Example: Amending After Pushing

bash
git commit --amend -m "Fix login bug"
git push --force-with-lease

Undoing an Amend Commit

Amended the wrong commit, or amended when you meant to make a new one? The reflog still has the previous commit's hash recorded from just before the amend — find it there and git reset --hard <hash> to get back to exactly where you were.

Example: Undoing an Amend Commit

bash
git reflog
git reset --hard a1b2c3d
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.