Fixing a Commit You Just Made
Right after committing, you notice a problem: a file you meant to include is
missing, or the message has a typo. If you have not shared the commit yet, you
can repair it with git commit --amend instead of adding a second commit that
only says "oops".
What Amend Does
git commit --amend replaces the newest commit on your branch with a new one.
The new commit is built the usual way, from what is staged, and it has the
same parent as the commit it replaces:
- To add a forgotten file or change, stage it, then amend.
- To change only the message, amend with nothing staged and give the new
message with
-m. - To keep the message, add
--no-edit. Without-mor--no-edit, Git opens your editor with the old message, ready to change.
git add <forgotten-file>
git commit --amend -m "<corrected message>" # new content and new message
git commit --amend --no-edit # new content, same message
The amended commit keeps the original author and author date, which is why Git's summary line repeats the old date. You will see that in the exercise.
Why the ID Changes
A commit's ID is calculated from everything in it: the snapshot, the message, the author, the time it was made and its parent. Amending changes some of these, so the result is a different commit with a different ID. Even an amend that changes only the message produces a new ID.
The old commit is not edited. Your branch simply moves to the new commit, and
the old one drops out of git log. Git keeps it for a while, and the recovery
module shows how to find it again.
Only Amend Unshared Commits
Amending is safe for commits that exist only on your computer. Once others have a commit, which happens when you share it (the remotes module covers this), replacing it causes trouble: their history contains the old commit, yours contains the new one, and the two no longer agree. Combining them afterwards takes extra work for everyone involved.
For a commit others already have, fix the problem with a new commit instead. The next lesson shows how to undo a shared commit safely.
Exercise: Repair the Last Commit
Your last commit was meant to add a plant list and a watering schedule. It contains only the plant list, and its message misspells "watering".
Starting state: Git configured as in the configuration lesson.
-
Run the setup:
mkdir -p ~/git-practice/amend-practice cd ~/git-practice/amend-practice git init -b main cat > README.md <<'EOF' # Garden notes EOF git add README.md git commit -m "Add README" cat > plants.md <<'EOF' # Plants - Tomatoes - Basil EOF cat > watering.md <<'EOF' # Watering Water the tomatoes every morning. Water the basil every other day. EOF git add plants.md git commit -m "Add plant list and waterring schedule" -
Inspect the mistake, and write down the newest commit's short ID:
git log --oneline git show --statIllustrative output from
git show --stat:commit b76f0c350ebf179312914a5bd9ba5ab8df3065c6 Author: Asha Rao <asha@example.com> Date: Fri Oct 9 16:24:57 2026 +0530 Add plant list and waterring schedule plants.md | 4 ++++ 1 file changed, 4 insertions(+)Only
plants.mdis in the commit.git statuswould listwatering.mdas untracked. -
Stage the forgotten file and amend with the corrected message:
git add watering.md git commit --amend -m "Add plant list and watering schedule"The summary line shows a new ID, and the
Date:line repeats the original commit's date, because the amended commit keeps it. -
Check the repair:
git log --oneline git show --statThere are still two commits. The newest has the corrected message and lists both files:
plants.md | 4 ++++ watering.md | 4 ++++ 2 files changed, 8 insertions(+)Compare the newest ID with the one you wrote down: it is different. The misspelled commit no longer appears in the log.
Success: git show --stat lists plants.md and watering.md under the
message "Add plant list and watering schedule", the log still has two commits,
and the newest commit ID has changed.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| The amended commit still lists one file | watering.md was not staged before amending | git add watering.md, then git commit --amend --no-edit |
| An editor opened | You ran --amend without -m or --no-edit | Edit the message, save and close; or close without changes to keep it |
| Three commits instead of two | You ran git commit without --amend | git log --oneline to see it; the reset lesson shows how to combine commits |
fatal: You have nothing to amend. | The repository has no commits yet | Make a normal commit first; amend only replaces an existing one |
-
Clean up:
cd ~/git-practice ls rm -rf amend-practice ls
Independent task: before cleaning up, change "Basil" to "Sweet basil" in
plants.md and include the fix in the last commit without changing its
message. Then show that the content changed, the message did not, and the
commit ID changed again.
Check Your Understanding
- You amend a commit and change only its message. Why does the commit get a different ID?
- A teammate already has your last commit. Should you amend it to fix a typo in a file? What should you do instead?
Answer and Reasoning
Independent task
Rewrite the file, stage it and amend without a new message:
cat > plants.md <<'EOF'
# Plants
- Tomatoes
- Sweet basil
EOF
git add plants.md
git commit --amend --no-edit
git log -1 still shows "Add plant list and watering schedule". git show
includes +- Sweet basil in the changes, and the ID differs from the one after
step 4. The log still has two commits.
Understanding check
- A commit's ID is calculated from its snapshot, message, author, time and parent. Changing the message changes what the ID is calculated from, so the amended commit is a new commit with a new ID. The old commit is unchanged; your branch just stops pointing at it.
- No. Amending replaces the commit with a new one, so your history would no longer match your teammate's. Fix the typo in a new commit, or undo the shared commit with the method in the next lesson.