Undo an accidental git commit --amend
Recover the original commit through the reflog, preserve the amended version, and separate the added changes into a new commit.
An amend creates a replacement commit. If the original object is still available, find it in the reflog and reset the branch back to it. A soft reset keeps the current index and working files, which lets you commit the amended changes separately.
Find and protect the relevant commits
git status
git reflog --date=iso
git show --stat ORIGINAL_COMMIT
git show --stat HEAD
Use the reflog's operations, dates, and diffs to identify the original commit. HEAD@{1} is only a candidate when no later operation changed HEAD; do not choose a target solely because it is the last non-amend message.
Create an unused rescue branch for the current amended commit, and save any unrelated uncommitted work separately:
git branch rescue-amended
# After verifying the original commit ID:
git reset --soft ORIGINAL_COMMIT
git diff --cached
git diff
Soft reset moves the branch but leaves both index and working tree as they were. Immediately after a clean amend, its content difference from the original commit is usually staged. If you have made later edits or staging changes, those states are retained too; review them rather than assuming the index contains only the amendment.
Make a separate commit or discard selected changes
To keep the amendment as its own commit, review the staged patch and run git commit -m "Describe the separate change".
To discard selected changes after saving anything needed, restore those paths from the recovered HEAD into both destinations:
git restore --source=HEAD --staged --worktree -- path/to/file
This is deliberately destructive for local changes at that path. Files introduced by the amendment can become untracked during some reset/restore workflows; inspect git status rather than assuming every new file has disappeared.
Other cases
If only the commit message is wrong, a new amend may be enough. git commit --amend --only -m "Correct message" changes the message without incorporating unrelated staged content. Review the resulting commit and remember that its ID changes again.
If the amended commit was published, coordinate before rewriting shared history. Restoring the old commit locally does not always require a force push: it depends on the current remote tip. When an agreed rewrite is necessary, use an appropriately checked lease and target branch; a new corrective commit may be preferable on a shared branch.
Reflogs are local and retention is configurable. Recovery depends on both the record and commit object still being available, not a guaranteed number of days. Save a named branch promptly once you find the right commit. Before future amendments, inspect git log -1 and git diff --cached to confirm both the commit and staged content you intend to replace.
See git-commit, git-reset, and git-reflog.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan