Skip to content

commandGit and releases

Undo Last Commit

Undoes the most recent commit safely, keeps your work, and picks between soft reset, mixed reset and revert for your case.

npx workspacetemplates add undo-last-commit --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/undo-last-commit.md18 lines · 1.2 KBFile
---
description: Undoes the most recent commit safely, keeps your work, and picks between soft reset, mixed reset and revert for your case.
argument-hint: "[what you want to keep, e.g. staged or unstaged]"
---

Undo the last commit. What the user wants: $ARGUMENTS (for example "keep changes staged", "it broke prod").

1. Run `git status`, `git log -n 3 --stat` and `git branch -vv`. Check whether the commit exists on the remote (`git branch -r --contains HEAD`).
2. Choose and explain:
   - Not pushed, want changes staged: `git reset --soft HEAD~1`.
   - Not pushed, want changes unstaged: `git reset --mixed HEAD~1`.
   - Only the message is wrong: tell the user `git commit --amend` fits, and do not run it unasked.
   - Pushed or shared: `git revert HEAD` so history is not rewritten.
   - Wrong branch: reset here, then offer `git switch` plus `git stash` or cherry-pick to move it.
3. Never run `git reset --hard` or `git clean`. If the working tree has other uncommitted edits, tell the user before resetting.
4. Print the hash of the commit being undone so it can be restored with `git reset --hard <hash>` or found in `git reflog`.

Show the commands, wait for approval, run them, then show `git status` and the final log.

What it does

The right way to undo a commit depends on whether it was pushed and whether you want the changes back. Choosing wrong either loses work or rewrites shared history. This command checks the facts first and explains the choice.

Your changes stay on disk in every path it takes, and it asks before any step that discards them.

Example

you › /undo-last-commit keep it staged

agent › HEAD is a1b2c3d "add export", not on any remote. Using git reset --soft HEAD~1: 4 files stay staged, nothing is lost. Restore with git reset --soft a1b2c3d if needed. Run it?

Questions

What if I already pushed?
It uses git revert, which adds a new commit that cancels the old one and does not rewrite shared history.
Will I lose my changes?
No. It avoids hard resets, and it prints the commit hash so the old state can be recovered from the reflog.
  • Branch Cleanup

    CommandGit and releases

    Free
  • Changelog Entry

    CommandGit and releases

    Free
  • Cherry Pick Fix

    CommandGit and releases

    Free
  • Commit Message

    CommandGit and releases

    Free