Recover lost workUsually recoverable

Undo git commit --amend and get the original commit back

I ran git commit --amend and destroyed the previous commit — can I get it back?

If you’re seeing this error

Your branch and 'origin/main' have diverged

You’re in the right place — the fix is below.

Short answer

Amend does not edit a commit, it replaces it — and the original is still in the object database, listed in the reflog. git reset --soft HEAD@{1} puts you back on it with your changes staged, and git reflog shows every version so you can pick the one you want.

quick fix
git reflog
git reset --soft HEAD@{1}

HEAD@{1} is where you were immediately before the amend

Does this match your situation?

  • You meant to make a new commit and typed --amend out of habit.
  • An amend swallowed changes from someone else's commit.
  • The commit message you overwrote is gone and you want it back.
  • 'Your branch and 'origin/main' have diverged' after amending something already pushed.

Step-by-step fix

  1. Read the reflog

    The amend is recorded as 'commit (amend)' and the entry directly below it is the commit as it was before. Nothing has been deleted — you are looking at both versions side by side.

    step 1
    git reflog
    # 8a3f9c2 HEAD@{0}: commit (amend): Fix login redirect
    # c71d0e5 HEAD@{1}: commit: Fix login redirect
    # 2b4a8d1 HEAD@{2}: checkout: moving from main to feature
    
  2. Look at the old commit before you move

    Confirm it is the one you want. The pre-amend commit is a complete, valid commit and you can inspect it exactly like any other.

    step 2
    git show HEAD@{1}
    git diff HEAD@{1} HEAD
    
  3. Go back to it, keeping your work

    --soft moves the branch pointer to the original commit and leaves everything the amend added staged in the index, so nothing is lost either way — you can commit it separately, which is usually what you meant to do.

    step 3
    git reset --soft HEAD@{1}
    git status        # the amended changes are staged
    git commit -m "The second change, as its own commit"
    
  4. Or discard the amend completely

    When the amend was simply a mistake and you want the original commit and nothing else, --hard is the direct route.

    step 4
    git reset --hard HEAD@{1}
    

    This discards the changes the amend introduced along with anything uncommitted in your working tree.

  5. Recover just the old commit message

    If all you lost was the wording, take it from the old commit without moving anything.

    step 5
    git log -1 --format=%B HEAD@{1} > /tmp/msg.txt
    git commit --amend -F /tmp/msg.txt
    
  6. If it was already pushed

    An amended commit has a different hash, so your branch and the remote have diverged. Force-push with lease, which refuses if anyone else has pushed since you last fetched.

    step 6
    git push --force-with-lease
    

    On a shared branch, tell people first — anyone who pulled the old commit will need to reset to the new history.

Why this works

Commits are immutable. --amend does not modify one; it builds a brand-new commit from your current index with the same parent, then moves the branch pointer to it. The original is untouched in .git/objects — it simply has nothing pointing at it any more, which in Git means unreachable rather than deleted. The reflog is what makes it findable: HEAD's journal records both positions, so HEAD@{1} still names the pre-amend commit for as long as the entry survives, which is 90 days by default. Even after that, git fsck can list the object until garbage collection actually prunes it.

If that didn’t work

  • git fsck --lost-found lists unreachable commits if the reflog entry has expired.
  • If you amended more than once, count further back: HEAD@{2}, HEAD@{3} and so on.
  • ORIG_HEAD is not set by amend, so the reflog is the reliable source here.
  • On a shared branch, another clone or the remote may still hold the original commit — fetch it and cherry-pick.

How to stop it happening again

  • Never amend a commit that has been pushed to a branch other people use.
  • Use git commit --amend --no-edit deliberately, and check git log -1 before pushing.
  • Configure a prompt or alias that shows the last commit hash so you notice when it changes.
  • Push with --force-with-lease rather than --force, always.

Commands used in this guide

git commit --amend

Replace the most recent commit instead of stacking another one on top.

git reflog

The undo history for everything - the single most important recovery command.

git reset

Move the current branch pointer, optionally rewriting the index and your files.

git fsck

Scan the object database for orphaned commits and blobs the reflog no longer lists.

git push

Upload your commits to the remote repository.

Still stuck?

Search the full command reference and every other rescue guide — there are 76 of them, covering everything from detached HEAD to force-push disasters.

Browse all guides →

Frequently asked questions

Can I undo git commit --amend?

Yes. The original commit still exists as an unreachable object and the reflog names it. git reset --soft HEAD@{1} returns your branch to it while keeping the amended changes staged, so you can commit them separately.

Does git commit --amend delete the old commit?

No — it creates a replacement and moves the branch pointer. The old commit stays in .git/objects with nothing referencing it, recoverable via the reflog for 90 days and via git fsck until garbage collection prunes it.

What does HEAD@{1} mean?

The position HEAD held one move ago, read from the reflog. HEAD@{0} is now, HEAD@{1} is immediately before the last operation — which after an amend is the original commit.

How do I recover just the commit message I overwrote?

git log -1 --format=%B HEAD@{1} prints the old message in full. Save it to a file and re-apply it with git commit --amend -F <file>, leaving the commit's content alone.

I amended a commit I had already pushed — what now?

Your branch has diverged from the remote because the commit has a new hash. Push with --force-with-lease, which aborts if anyone else pushed in the meantime, and tell collaborators so they can reset rather than merging the old and new versions together.

Related rescue guides

← All Git Rescue guides