AI Hints
Home›Git & GitHub Tutorial›Undoing Changes
Git & GitHub · Lesson 26 of 144

Fixing the Last Commit

Git commit --amend.

⏱ 2 min read▶ Video at 27:08🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

You just committed, and immediately noticed a typo in the message. Or you forgot to include a file. You don't need a whole new commit. You can fix the last one.

Fixing the Last Commit
From the video: Fixing the Last Commit
  • A typo in the message
  • A forgotten file
  • Fix the last commit

Fixing a message

Look at the log. Our last message says add contcat page, with a typo. Git commit dash dash amend, with dash m and the right message, replaces it. The log now shows the fixed message.

git
git log --oneline -n 2
bc9879a (HEAD -> main) Add contcat page
f63a17a Link to about page
git commit --amend -m "Add contact page"
[main e30203e] Add contact page
 Date: Mon Jan 5 09:29:41 2026 +0000
 1 file changed, 1 insertion(+)
 create mode 100644 contact.html
git log --oneline -n 2
e30203e (HEAD -> main) Add contact page
f63a17a Link to about page

Adding a forgotten file

Now we forgot the contact page's style file. We stage it, then amend with dash dash no edit, which keeps the same message. Show dash dash stat proves the last commit now contains both files.

git
git add contact.css
git commit --amend --no-edit
[main 3c7861d] Add contact page
 Date: Mon Jan 5 09:29:41 2026 +0000
 2 files changed, 2 insertions(+)
 create mode 100644 contact.css
 create mode 100644 contact.html
git show --stat --oneline
3c7861d (HEAD -> main) Add contact page
 contact.css  | 1 +
 contact.html | 1 +
 2 files changed, 2 insertions(+)

One rule

Amend actually replaces the last commit with a brand new one. That's why the hash changes. So only amend commits that you haven't shared with anyone yet. Once you've pushed to GitHub, leave them alone.

Amendreplaces the last commit
New hashit's a new commit
Safebefore you share it
After pushingdon't amend

Summary

git commit dash dash amend fixes the last commit's message or contents. Only amend what you haven't shared. Next, bringing back an old version of a file.

Key takeaways
  • --amend -m "new message"
  • --amend --no-edit
  • Only before sharing
Next up: Old Versions of a File — git restore --source