undo the last git commit

Take back a commit, keep its changes or not, fix its message, or undo one already pushed.

  • version-control

Steps

  1. git reset --soft HEAD~1

    undo it, keeping its changes staged

  2. git reset HEAD~1

    undo it, keeping the changes, unstaged

  3. git commit --amend

    change its message, or add what you forgot (git add first)

  4. git revert HEAD

    already pushed: a new commit that undoes it

  5. git reflog

    every place HEAD was: find a commit you lost

Notes

  • Do not reset or amend commits others already pulled; revert them.

Commands used

  • git

Each has a page in lore: lore git opens it.