GitAmending Commits (--amend)

Amending Commits (--amend)

git commit --amend is the tidy-up command for your last commit. It replaces that commit with a new one — same parent, but with whatever message, files, or metadata you just changed. The original commit becomes unreachable and disappears from normal git log output (though the reflog still has it).

What --amend actually does

Amend creates a new commit and moves the branch pointer

Text
Before:                 A ─ B  ◀── main
                              ▲
                              HEAD

git commit --amend
                        A ─ B'  ◀── main
                              ▲
                              HEAD       (B is now unreachable; B' has a new SHA)
Recipe 1: change just the message

Fix a typo in the last commit message

Bash
# Opens your editor with the existing message pre-filled
git commit --amend

# Or set the new message inline
git commit --amend -m "Fix login validation regex"
Recipe 2: add a forgotten file

Tack a file onto the last commit, keep the message

Bash
git add forgotten.test.js
git commit --amend --no-edit
# The file is now part of the previous commit; message unchanged.
Recipe 3: replace the entire commit content

Restage, then amend

Bash
# You realised the staged content was wrong
git restore --staged .
# Re-add what you actually want
git add src/correct-file.js
git commit --amend --no-edit
# The previous commit is replaced; same message, new content.
Changing the author

Fix attribution

Bash
git commit --amend --author="Alice Smith <alice@example.com>"
# Also opens the editor — pass --no-edit if the message is fine
Changing the timestamp

Backdate or update the date

Bash
# A specific moment
git commit --amend --date="2026-05-19T10:00:00"

# Reset the author date to right now
git commit --amend --date="now" --no-edit
Inspecting before and after

Confirm the amend

Bash
git log -1 --pretty=fuller
# commit b3f4a2c... (HEAD -> main)
# Author:     You
# AuthorDate: ...
# Commit:     You
# CommitDate: ...

git reflog
# b3f4a2c HEAD@{0}: commit (amend): Fix login validation regex
# 9c1a2b3 HEAD@{1}: commit: Fix logn vlaidation regex   ← original
The history-rewriting catch
Warning
`--amend` is **history rewriting**. The amended commit has a new SHA, so if the original commit was already pushed, the remote still has the old SHA. Pushing now will be rejected with “non-fast-forward.” You will need `git push --force-with-lease`, and any teammate who pulled the old commit will need to reset to your new one.

The post-amend push dance

Bash
# Amended a commit that was already pushed?
git push --force-with-lease
# --force-with-lease refuses to overwrite if origin advanced since
# your last fetch — safer than plain --force.
Amend only works on the LAST commit

To fix an older commit, use interactive rebase with the fixup workflow: make a tiny fix commit, then git rebase -i to fold it into the older commit.

The fixup recipe for older commits

Bash
# Make a fixup commit targeting the old commit
git add forgotten.js
git commit --fixup=9c1a2b3

# Then squash it into place
git rebase -i --autosquash 9c1a2b3~1
# The editor opens with the fixup commit already marked &apos;fixup&apos;
# Save and close; the fixup merges into 9c1a2b3 with no message edit.
When NOT to amend
  • The commit was merged into main or any shared branch — amend would create divergent history. Use git revert instead.

  • You are mid-merge or mid-rebase — finish the operation first.

  • You actually want a separate commit for the new work — amend is for tidy-ups, not for piling unrelated changes into the last commit.

Mental model
`git commit --amend` is `git reset --soft HEAD~1 && git commit` in one step — it rewinds the branch by one, then commits whatever is currently staged with the existing message and metadata (unless you override them).
Tip
Train yourself to use `--no-edit` whenever you only changed the files, not the message. It saves an editor round-trip and signals to reviewers (in the reflog) that the message was deliberately preserved.