Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Git & Version Controlintermediate

Git Undo

Share

Safely undo the last git operation (commit, merge, rebase, etc.)

Works with OpenClaude

You 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 reflog to see the complete history of HEAD changes — this shows every operation, not just commits
  • Run git status to see if there are uncommitted changes that might be affected
  • Run git log --oneline -5 to confirm which commit you're currently on

Steps

  1. 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
  2. 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
  3. For undoing the last commit (keep changes unstaged): run git reset --mixed HEAD~1 — files return to working directory, not staged
  4. 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
  5. For undoing a merge: run git reset --hard HEAD~1 if the merge is the last operation, or use git merge --abort if merge is still in progress
  6. For undoing a rebase in progress: run git rebase --abort to stop the rebase and return to the previous state
  7. 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
  8. Verify the undo worked: run git log --oneline -5 and git status to 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

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
gitundosafety

Install command:

curl -o ~/.claude/skills/git-undo.md https://claude-skills-hub.vercel.app/skills/git/git-undo.md

Related Git & Version Control Skills

Other Claude Code skills in the same category — free to download.

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.