iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Amending Commits

`git commit --amend` rewrites the last commit. Use it to add forgotten files, fix the message, or squash a tiny follow-up into the commit it belongs to — BEFORE you push. After publishing, amend means rewriting shared history; reach for revert or fixup instead.

amend safely, plus fixup + autosquash

EXAMPLE
# 1) Add a forgotten file to the last commit
git add forgot.go
git commit --amend --no-edit

# 2) Fix the last commit's message
git commit --amend                       # opens editor
git commit --amend -m 'feat: support partial refunds'   # one-shot

# 3) Amend the author / date if you really need to
git commit --amend --author='Alice <alice@example.com>'
git commit --amend --date='2026-06-18 09:00:00 +1000'

# 4) Amend WITHOUT editing the message AND keep the timestamp
git commit --amend --no-edit

# 5) Stop being scary — amend with verification
git commit --amend --verbose             # show diff in editor

# ===== When NOT to amend =====
# The previous commit is PUBLISHED (pushed). Amending rewrites the SHA;
# teammates already have the old commit and force-push leaves them with
# orphans + broken history. Use 'git revert' or 'git commit --fixup' (next).

# ===== Better: fixup + autosquash =====
# A clean way to fix a commit that is already on a branch.

# 1) Make the small fix as a SEPARATE commit, marked as a fixup of an earlier one
git add fixed-file.go
git commit --fixup=<sha-of-the-commit-you-fix>     # message starts 'fixup! ...'

# 2) Continue working with more commits if you want

# 3) Before opening the PR, squash the fixups in
git rebase -i --autosquash main
# Editor opens with the fixup commits pre-marked 'fixup'.
# Just save and quit; they fold into the right places.

# ===== Other 'how do I fix the last commit?' recipes =====

# a) 'I committed wrong files' — pull them out
git restore --staged --worktree path/to/file
git commit --amend --no-edit

# b) 'I committed to the wrong branch'
git log -1 --oneline          # remember the SHA
git reset HEAD~1              # uncommit on current branch (changes stay)
git switch correct-branch
git commit -m 'feat: ...'

# c) 'I want to split the last commit into two'
git reset HEAD~1              # uncommit, changes stay
git add part-a/*.go
git commit -m 'feat: part A'
git add part-b/*.go
git commit -m 'feat: part B'

# d) 'I want to combine the last N commits into one (locally)'
git reset --soft HEAD~3
git commit -m 'chore: squash WIP into a single commit'

# e) 'I rebased my branch, force-push?' — yes, but to YOUR branch, not main:
git push --force-with-lease   # 'with-lease' refuses to clobber someone else's push

# ===== Conventional commit messages pair well with --amend =====
# feat(orders): support partial refunds
# fix(checkout): handle empty postal code
# refactor(db): extract repository
# chore(deps): bump zod to 3.23
# Use them and tooling (release-please / changesets) drives versioning + notes.

# ===== Safety net: pre-amend bookmark =====
git tag _SAFETY HEAD
# ... amend, rebase, whatever ...
# If things go wrong:
git reset --hard _SAFETY
git tag -d _SAFETY

Why it matters

Amend the last commit locally; everywhere else, prefer `git commit --fixup` + `git rebase -i --autosquash`. The fixup workflow turns "I need to edit my history without confusing my teammates" into a two-command operation that stays local until the squash, after which the branch looks like you got it right the first time.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git commit --amend -m 'better message'
git commit --amend --no-edit    # tack staged changes onto last commit
Try it Yourself »

Discussion

Loading…