Skip to content

Git Amend Commit — Change Last Commit Fix

DodaTech Updated 2026-06-24 3 min read

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-lease instead of --force when pushing amended commits
  • Use git log to check if the commit has been shared with others
  • Consider git commit --fixup + git rebase -i for older commits

Common Mistakes with amend commit

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. 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

### What is the difference between git commit --amend and a new commit?

git commit --amend replaces the last commit with a new one that has a different SHA. This rewrites history. A new commit appends to the existing history. Use amend for local, unpublished changes only.

Why does git push reject an amended commit?

Amending replaces the commit with a new SHA. Git rejects non-fast-forward pushes because other collaborators may have based work on the old commit. Use git push --force-with-lease if you are certain no one else is using the branch.

Can I amend a commit that is not the most recent?

No, --amend only works on the most recent commit (HEAD). To change an older commit, use git rebase -i HEAD~N and mark the commit for reword (message only) or edit (content + message).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro