Git Amend

Updated · 10 min read
mediumGit 2.53gitgit-amendreset-authorrewrite-historyinterview

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

Terminal
git commit --amend [options]
OptionDescription
--amendIt 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-editIt is used to keep the message as it is.
-aIt 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-authorIt 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-lease and care for pushed ones.
  • The old commit isn't deleted at once. It stays reachable through the reflog for a while.

The Starting Point

Terminal
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 -s
Output
0254c3e (HEAD -> main) Ad the late fe
6b651a0 Add the term fee
?? LateFee.java

Two mistakes in one commit: the message has typing errors, and LateFee.java was forgotten.

Example 1: Fix the Message

Terminal
git commit --amend -m "Add the late fee"
git log --oneline
Output
[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 fee

Explanation: 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:

Terminal
git add LateFee.java
git commit --amend --no-edit
git show --stat --oneline HEAD
Output
[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:

Terminal
git commit --amend --no-edit --author="Advitha Anamaneni <advitha@example.com>"
git log -1 --format="Author:    %an <%ae>%nCommitter: %cn <%ce>"
Output
[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:

Terminal
git commit --amend --no-edit --reset-author
git log -1 --format="Author:    %an <%ae>"
Output
[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:

Terminal
git reflog -3
Output
6868351 (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 fee

git 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}:

Terminal
git reset --soft HEAD@{1}
git log -1 --format="%h  Author: %an"
Output
1d2197f  Author: Advitha Anamaneni

Explanation: HEAD@{1} was the commit before --reset-author, with Advitha as the author. The branch points to it again.

An amend replaces the commit; the reflog keeps the old one

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
Terminal
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 -s
Output
Add the hostel fee
M  fees.txt

After 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}.

Quick check: Git amend

4 questions
1What does git commit --amend create?
2How do you add a forgotten file to the last commit without changing the message?
3What does --reset-author change?
4Which command undoes the last amend and keeps the content staged?

Key Takeaways

Quick reference

CommandIt is used to
git commit --amend -m "<message>"Fix the last message
git commit --amend --no-editAdd staged changes to the last commit
git commit -a --amend --no-editAdd all tracked changes to the last commit
git commit --amend --no-edit --author="Name <email>"Change the author
git commit --amend --no-edit --reset-authorMake yourself the author
git reset --soft HEAD@{1}Undo the last amend

Was this tutorial helpful?

Lesson 57 of 106