Git Amend
Amending in Git means replacing the most recent commit with a corrected version of it. The command git commit --amend takes the last commit, combines it with whatever is staged now, lets you change the message, and saves the result as a new commit in its place. It is used to fix small mistakes right after committing: a typing error in the message, a forgotten file, or the wrong author.
It works like reprinting the last page of a letter before posting it. You don't add a page that says "correction to the previous page". You print a fixed page and throw the old one away. Nobody else has seen the old page, so nobody is confused.
"Git Commit" introduced --amend. This lesson covers all of its uses, and how to undo an amend.
Syntax
git commit --amend [options]| Option | Description |
|---|---|
--amend | It is used to replace the last commit, opening the editor with its message. |
-m "<message>" | It is used to give the new message directly. |
--no-edit | It is used to keep the message as it is. |
-a | It is used to stage all changes to tracked files first, as with a normal commit. |
--author="Name <email>" | It is used to set a different author. |
--reset-author | It is used to make you the author, with the current time. |
--date="<date>" | It is used to set a different author date. |
Features of Amend
- It always makes a new commit. The commit ID changes, even if only the message changed.
- It works only on the last commit. Older commits are changed with interactive rebase or fixup commits (the next two lessons).
- It rewrites history. That is harmless for unpushed commits, and needs
--force-with-leaseand care for pushed ones. - The old commit isn't deleted at once. It stays reachable through the reflog for a while.
The Starting Point
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
echo 'Late fee: $250' >> fees.txt
echo "class LateFee {}" > LateFee.java
git add fees.txt
git commit --quiet -m "Ad the late fe"
git log --oneline
git status -s0254c3e (HEAD -> main) Ad the late fe
6b651a0 Add the term fee
?? LateFee.javaTwo mistakes in one commit: the message has typing errors, and LateFee.java was forgotten.
Example 1: Fix the Message
git commit --amend -m "Add the late fee"
git log --oneline[main 8eb83bd] Add the late fee
Date: Mon Jan 5 10:10:00 2026 -0600
1 file changed, 1 insertion(+)
8eb83bd (HEAD -> main) Add the late fee
6b651a0 Add the term feeExplanation: the last commit was replaced. The message is fixed, and the ID changed. There are still two commits, not three. The Date: line shows the original author date, because amending keeps it (it is the time the change was first written).
Example 2: Add a Forgotten File
Stage the file, then amend with --no-edit to keep the message:
git add LateFee.java
git commit --amend --no-edit
git show --stat --oneline HEAD[main 6ecc577] Add the late fee
Date: Mon Jan 5 10:10:00 2026 -0600
2 files changed, 2 insertions(+)
create mode 100644 LateFee.java
6ecc577 (HEAD -> main) Add the late fee
LateFee.java | 1 +
fees.txt | 1 +
2 files changed, 2 insertions(+)Explanation: the commit now contains both files. Anything staged is added to the last commit by --amend. To remove a file from the last commit, take it out of the staging area for the next snapshot with git rm --cached <file> (it stays in your folder), then amend.
Example 3: Fix the Author
Advitha wrote the late fee change, but Navika committed it on her computer. The author can be corrected:
git commit --amend --no-edit --author="Advitha Anamaneni <advitha@example.com>"
git log -1 --format="Author: %an <%ae>%nCommitter: %cn <%ce>"[main 1d2197f] Add the late fee
Author: Advitha Anamaneni <advitha@example.com>
Date: Mon Jan 5 10:10:00 2026 -0600
2 files changed, 2 insertions(+)
create mode 100644 LateFee.java
Author: Advitha Anamaneni <advitha@example.com>
Committer: Navika Anamaneni <navika@example.com>Explanation: the author is now Advitha, the person who wrote the change. The committer is still Navika, the person who made this commit object. That is exactly what the two fields are for (see "Git Commit", Under the Hood).
--reset-author does the opposite: it makes you the author and resets the author date to now. Use it, for example, after you have rewritten someone's commit so much that it is really yours, or when your user.email was wrong when you committed:
git commit --amend --no-edit --reset-author
git log -1 --format="Author: %an <%ae>"[main 6868351] Add the late fee
2 files changed, 2 insertions(+)
create mode 100644 LateFee.java
Author: Navika Anamaneni <navika@example.com>Undo an Amend
Amended the wrong thing? The previous version of the commit is still in the reflog, one step back. HEAD@{1} is where HEAD was before the last amend:
git reflog -36868351 (HEAD -> main) HEAD@{0}: commit (amend): Add the late fee
1d2197f HEAD@{1}: commit (amend): Add the late fee
6ecc577 HEAD@{2}: commit (amend): Add the late feegit reset --soft HEAD@{1} moves the branch back to the commit before the last amend, and keeps the current content staged. To go back over several amends, use the matching reflog entry, such as HEAD@{2}:
git reset --soft HEAD@{1}
git log -1 --format="%h Author: %an"1d2197f Author: Advitha AnamaneniExplanation: HEAD@{1} was the commit before --reset-author, with Advitha as the author. The branch points to it again.
Amending a Pushed Commit
Common Mistakes
Amending when you meant to make a new commit. Your last commit silently grows. Run git status and git log -1 before --amend.
Staging extra changes by accident before amending. They become part of the last commit. Check git diff --staged first.
Amending a commit someone already pulled. Their history no longer matches. Make a new commit instead.
Expecting --amend to change an older commit. It only affects HEAD. Use a fixup commit and autosquash, or interactive rebase.
Using --reset-author when you only meant to fix the message. It replaces the original author's name. Use plain --amend for the message.
Under the Hood
git commit --amend builds a new commit object whose parent is the old commit's parent, not the old commit itself. Its tree is the current staging area, and its message is the old or the new message. Then it moves the branch to it. The old commit is left without a branch, but the reflog still lists it, which is how the undo above works. Because the commit ID is calculated from all of this content, even a one-letter change to the message produces a completely different ID.
Interview Corner
What does git commit --amend do?
It replaces the most recent commit with a new commit that has the same parent, the current staged content and an optionally new message. The old commit is no longer on the branch, and the new commit has a new ID.
How do you add a forgotten file to the last commit?
git add <file> and then git commit --amend --no-edit.
How do you change the author of the last commit?
git commit --amend --no-edit --author="Name <email>", or --reset-author to make yourself the author with the current date.
How do you undo an amend?
git reset --soft HEAD@{1}, using the reflog entry from before the amend. The branch returns to the previous version of the commit, and the current content stays staged.
Is it safe to amend a pushed commit?
Only on a branch nobody else uses, followed by git push --force-with-lease. On shared branches, add a new commit or revert instead.
Practice
Challenge: amend twice, undo once. Add Hostel fee: $9,000 to fees.txt and commit it as "Add hostel". Then amend the message to "Add the hostel fee". Then amend again, this time also adding the line Exam fee: $800. Finally, undo only the last amend, and print the last commit's message and the short status.
What does Git print? Before you open the solution, decide which message the last commit has after the undo, and whether the exam fee line is staged, committed or gone.
Show solution
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add hostel"
git commit --quiet --amend -m "Add the hostel fee"
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -a --amend --no-edit
git reset --soft HEAD@{1}
git log -1 --format=%s
git status -sAdd the hostel fee
M fees.txtAfter the undo, the last commit is the one with the corrected message, "Add the hostel fee", and without the exam fee. --soft kept the exam fee line, now staged (M ), ready to commit again or to amend once more.
Checkpoint. In a practice repository, make a commit with a typing error and a forgotten file. Fix both with --amend, then change the author with --author, and finally undo the author change with git reset --soft HEAD@{1}.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git commit --amend -m "<message>" | Fix the last message |
git commit --amend --no-edit | Add staged changes to the last commit |
git commit -a --amend --no-edit | Add all tracked changes to the last commit |
git commit --amend --no-edit --author="Name <email>" | Change the author |
git commit --amend --no-edit --reset-author | Make yourself the author |
git reset --soft HEAD@{1} | Undo the last amend |
Was this tutorial helpful?