Git Amend: Modify the Last Commit

Use git commit --amend to change the last commit message, add forgotten files, or modify the most recent commit without creating a new one.

git commit --amend -m "Updated commit message"

Detailed Explanation

What Does git commit --amend Do?

git commit --amend replaces the most recent commit with a new one. You can change the commit message, add forgotten files, or both. The original commit is discarded and a new commit with a new hash takes its place.

Changing the Commit Message

git commit --amend -m "Updated commit message"

Or omit -m to open your editor with the previous message pre-filled.

Adding Forgotten Files

# Stage the forgotten file
git add src/utils/helpers.ts

# Amend the commit to include it
git commit --amend --no-edit

The --no-edit flag keeps the original commit message unchanged.

Changing the Author

git commit --amend --author="Jane Doe <jane@example.com>"

How It Works Internally

Amend does not actually "edit" the commit. Git creates an entirely new commit object with the updated contents and points the branch to it. The old commit becomes orphaned (recoverable via git reflog until garbage-collected).

Important Caveats

Do not amend commits that have been pushed to a shared branch. Since amend changes the commit hash, it rewrites history. If you must amend a pushed commit on your own feature branch, follow up with git push --force-with-lease.

Common Workflow

# Make your commit
git commit -m "Add user authentication"

# Realize you forgot a file
git add src/middleware/auth.ts
git commit --amend --no-edit

# Realize the message needs updating
git commit --amend -m "Add user authentication middleware"

Think of --amend as a quick "redo" button for your last commit. For anything older, use interactive rebase instead.

Use Case

A developer realizes they forgot to include a test file in their last commit. They stage the file and amend the commit before pushing.

Try It — Git Command Builder

Open full tool