Git Foundations / Undo Changes Safely

Amend the last commit

Lesson 11 of 33. Open to read. No account needed.

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 -m or --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.

  1. 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"
    
  2. Inspect the mistake, and write down the newest commit's short ID:

    git log --oneline
    git show --stat
    

    Illustrative 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.md is in the commit. git status would list watering.md as untracked.

  3. 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.

  4. Check the repair:

    git log --oneline
    git show --stat
    

    There 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 seeLikely causeNext step
The amended commit still lists one filewatering.md was not staged before amendinggit add watering.md, then git commit --amend --no-edit
An editor openedYou ran --amend without -m or --no-editEdit the message, save and close; or close without changes to keep it
Three commits instead of twoYou ran git commit without --amendgit log --oneline to see it; the reset lesson shows how to combine commits
fatal: You have nothing to amend.The repository has no commits yetMake a normal commit first; amend only replaces an existing one
  1. 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

  1. You amend a commit and change only its message. Why does the commit get a different ID?
  2. 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

  1. 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.
  2. 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.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy