Safely undo the last git operation (commit, merge, rebase, etc.)
✓Works with OpenClaudeYou are a Git version control expert. The user wants to safely undo their last Git operation (commit, merge, rebase, or push) without losing work.
What to check first
- Run
git reflogto see the complete history of HEAD changes — this shows every operation, not just commits - Run
git statusto see if there are uncommitted changes that might be affected - Run
git log --oneline -5to confirm which commit you're currently on
Steps
- Identify what operation you need to undo by checking
git reflog— note the SHA or HEAD@{n} reference of the state you want to return to - For undoing the last commit (keep changes staged): run
git reset --soft HEAD~1— this moves HEAD back one commit but keeps your changes in the staging area - For undoing the last commit (keep changes unstaged): run
git reset --mixed HEAD~1— files return to working directory, not staged - For undoing the last commit (discard all changes): run
git reset --hard HEAD~1— use only if you're certain you don't need those changes - For undoing a merge: run
git reset --hard HEAD~1if the merge is the last operation, or usegit merge --abortif merge is still in progress - For undoing a rebase in progress: run
git rebase --abortto stop the rebase and return to the previous state - For undoing a pushed commit (already on remote): run
git revert <commit-sha>to create a new commit that undoes the changes — safer than force-push - Verify the undo worked: run
git log --oneline -5andgit statusto confirm HEAD is where you expect
Code
#!/bin/bash
# Git Undo Helper - safely undo the last operation
set -e
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${YELLOW}Git Undo Helper${NC}"
echo "================="
echo ""
# Show recent operations
echo -e "${YELLOW}Recent git operations (reflog):${NC}"
git reflog -n 5 --pretty=format:"%h %gd: %gs"
echo ""
# Show current state
echo -e "${YELLOW}Current HEAD:${NC}"
git log -1 --oneline
echo ""
# Ask user what to undo
echo -e "${YELLOW}What do you want to undo?${NC}"
echo "1) Last commit (keep changes)"
echo "2) Last commit (discard changes)"
echo "3) Last merge"
echo "4) Last rebase"
echo "5) Last push (create revert commit)"
echo "6) Go to specific point in reflog"
read -p "Enter choice (1-6): " choice
case $choice in
1)
echo
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Git & Version Control Skills
Other Claude Code skills in the same category — free to download.
Smart Commit
Generate conventional commit messages by analyzing staged changes
Branch Cleanup
Find and delete merged/stale local and remote branches
Changelog Generator
Generate CHANGELOG.md from git history using conventional commits
Conflict Resolver
Analyze and suggest resolutions for merge conflicts
Git Bisect Helper
Automate git bisect to find the commit that introduced a bug
PR Description
Generate detailed PR descriptions from branch diff
Commit Splitter
Split a large commit into smaller, logical commits
Git Hooks Setup
Set up pre-commit, pre-push, and commit-msg hooks
Want a Git & Version Control skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.