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

Git amend Commit

Amending a commit is like fixing a typo on the caption of the photo you just glued in, instead of pasting a second photo.
Syntax
bash
git commit --amend -m "new message"
git commit --amend --no-edit

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Amending a commit that is already pushed and then getting a rejected push, since history changed and would need a force push.
  2. Forgetting to git add the file before git commit --amend --no-edit, so the amended commit still lacks it.
  3. Running git commit --amend with no message flag and being dropped into an editor without expecting it.
🔒

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.