Git Amend Commit — Change Last Commit Fix
In this tutorial, you'll learn about Git Amend Commit. We cover key concepts, practical examples, and best practices.
git commit --amend modifies the most recent commit by updating its message or including staged changes. Errors occur when you amend a pushed commit (divergent history), amend without staging, or try to amend when there is nothing to change.
The Problem
git commit --amend
Opens editor with the previous commit message, but then:
error: There is nothing to amend.
Or after amending a pushed commit:
git push origin main
Error:
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'origin'
hint: Updates were rejected because the tip of your current branch is behind...
Wrong Approach
# WRONG — amending without adding files first
git commit --amend # No changes staged — fails
Right Approach
# Stage new changes first, then amend
git add forgotten-file.txt
git commit --amend -m "Updated commit message with all changes"
Expected output:
[main abc123] Updated commit message with all changes
Date: Wed Jun 24 10:30:00 2026 +0000
2 files changed, 10 insertions(+), 2 deletions(-)
Step-by-Step Fix
Step 1: Stage changes you want to include
git add forgotten-file.txt
Step 2: Amend with a new message
git commit --amend -m "New commit message"
Step 3: Amend without changing the message
git add more-changes.txt
git commit --amend --no-edit
Step 4: Amend only the message (no file changes)
git commit --amend -m "Corrected commit message"
Step 5: Handle pushed commits
# Force push if you are the only person working on the branch
git push --force-with-lease origin main
Step 6: View the amended history
git log --oneline -3
Prevention Tips
- Stage all changes with
git add .before amending - Only amend commits that have not been pushed yet
- Use
--force-with-leaseinstead of--forcewhen pushing amended commits - Use
git logto check if the commit has been shared with others - Consider
git commit --fixup+git rebase -ifor older commits
Common Mistakes with amend commit
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro