agentleFS
nguyenthanhtatnguyenthanhtat/screen1-claude/skills/git/undo/SKILL.md

undo

Skill2 starsChanged 21 days agoMITView on GitHub

Parent Skill: /git Path: /git/undo Trigger automatically when: - User mentions: undo, reset, revert, restore, reflog, recover, amend, rollback - Made mistake in commit - Need to unstage files - Want to discard changes - Recover deleted commits - Fix commit history Chat commands: Problem: Staged file by mistake Before and after: Old vs new syntax: Impact: Remove file from staging without losing changes When to use: - Staged wrong file - Want to commit files separately - Accidentally staged…

  • Reads credentials
  • Deletes or force-pushes
  • Commits and pushes

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

14 rules. 0 are common, 14 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 14

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  • could be automatic: a setting could enforce it
  1. Distinctive:

    NEVER use git reset --hard without backup if unsure

    line 54
  2. Distinctive:

    NEVER force push reset to shared branches

    line 55
  3. Distinctive:

    ALWAYS verify what will be affected before undo

    line 56
  4. Distinctive:

    Use git revert for public history (not reset)

    line 57
  5. Distinctive:

    ALWAYS check git status before major undo operations

    line 58
  6. Distinctive:

    Impact: Remove file from staging without losing changes

    line 130
  7. Distinctive:

    Problem: Don't want local modifications

    line 142
  8. Distinctive:

    After pushing own branch (with force-with-lease)

    line 278
  9. Distinctive:

    Start over from specific point (hard)

    line 365
  10. Distinctive:

    Never on public branches!

    line 366
  11. Distinctive:

    Check status first: - Always git status before major undo

    line 744
  12. Distinctive:

    Prefer revert for public: - Use revert on shared branches

    line 745
  13. Distinctive:

    Reset for private only: - Only reset on your own branches

    line 746
  14. Distinctive:

    Backup before hard reset: - Create branch or stash

    line 747

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Git Undo

Parent Skill: /git Path: /git/undo

Purpose

Master undoing changes at every stage: unstage files, discard working changes, amend commits, reset history, revert safely, and recover lost commits with reflog.

When to Use

Trigger automatically when:

  • User mentions: undo, reset, revert, restore, reflog, recover, amend, rollback
  • Made mistake in commit
  • Need to unstage files
  • Want to discard changes
  • Recover deleted commits
  • Fix commit history

Chat commands:

/git/undo unstage this file
/git/undo discard my local changes
/git/undo fix my last commit message
/git/undo undo last commit but keep changes
/git/undo recover deleted commit

Requirements

<critical> - Git repository with commit history (for most operations) - Understanding of Git states (working/staging/committed) - For recovery: Commits not garbage collected yet - CAUTION: Some operations destructive and irreversible </critical>

Verification

# Check current state
git status

# See commit history
git log --oneline -5

# Check reflog (for recovery)
git reflog

Critical Rules

<critical> 1. NEVER use `git reset --hard` without backup if unsure 2. NEVER force push reset to shared branches 3. ALWAYS verify what will be affected before undo 4. Use `git revert` for public history (not reset) 5. ALWAYS check `git status` before major undo operations 6. Reflog expires after 90 days - recover deleted commits ASAP </critical>

Undo Workflow

File States & Undo Commands:

Working Directory → Staging → Repository
   (modified)       (staged)   (committed)
       ↓               ↓            ↓
 git restore      git restore   git reset
                   --staged    or git revert

Recovery: git reflog (for any deleted commits)

Patterns

Pattern 1: Unstage Files (Restore --staged)

Problem: Staged file by mistake

# ❌ BAD: Old confusing syntax
git reset HEAD file.js  # Works but unclear

# ✅ GOOD: Modern, clear syntax (Git 2.23+)
git restore --staged file.js

# Unstage all files
git restore --staged .

# Unstage specific files
git restore --staged src/app.js src/utils.js

Before and after:

# Accidentally staged everything
git add .
git status
# Changes to be committed:
#   modified: src/app.js
#   modified: .env  # Oops! Secret file!

# Unstage .env
git restore --staged .env

git status
# Changes to be committed:
#   modified: src/app.js
# Changes not staged:
#   modified: .env  # Back to unstaged

Old vs new syntax:

# Old way (still works)
git reset HEAD file.js

# New way (clearer intent)
git restore --staged file.js

# Both do same thing: unstage file

Impact: Remove file from staging without losing changes

When to use:

  • Staged wrong file
  • Want to commit files separately
  • Accidentally staged secrets
  • Reviewing what to commit

Pattern 2: Discard Working Directory Changes (Restore)

Problem: Don't want local modifications

# ❌ DANGEROUS: Old confusing syntax
git checkout -- file.js  # Confusing with branch checkout

# ✅ GOOD: Modern, clear syntax
git restore file.js

# Discard all changes
git restore .

# Discard specific files
git restore src/app.js src/utils.js

# Interactive discard (choose hunks)
git restore -p file.js

Warning - This is destructive!

# Before restore
git status
# Changes not staged for commit:
#   modified: src/app.js  # Hours of work!

# ❌ DANGER: Lost forever if not staged!
git restore src/app.js

# After restore: changes gone!
git status
# nothing to commit, working tree clean

# CANNOT UNDO THIS! Changes lost!

Safe workflow:

# Save changes first (just in case)
git stash

# Or commit to temporary branch
git checkout -b backup-$(date +%Y%m%d-%H%M%S)
git add .
git commit -m "Backup before restore"
git checkout original-branch

# Then restore if still needed
git restore file.js

Restore from specific commit:

# Restore file from specific commit
git restore --source=abc1234 file.js

# Restore from main branch
git restore --source=main file.js

# Restore file to staged state
git restore --staged --worktree file.js

Impact: Clean working directory, but changes lost

When to use:

  • Experimental changes failed
  • Want to start over
  • Undo local edits
  • Match repository state

Pattern 3: Amend Last Commit

Problem: Typo in commit message or forgot to include file

# ❌ BAD: Create new commit for minor fix
git commit -m "Add user authentification"  # Typo!
git commit -m "Fix typo in commit message"  # Messy history

# ✅ GOOD: Amend last commit
git commit -m "Add user authentification"  # Typo!
# Fix: Amend instead
git commit --amend -m "Add user authentication"

Forgot to include file:

# Made commit
git commit -m "Add user authentication"

# Oops, forgot a file!
git add src/forgot-this.js

# Add to last commit
git commit --amend --no-edit
# --no-edit keeps same message

Change last commit message:

# Change message interactively
git commit --amend
# Editor opens, change message, save

# Change message inline
git commit --amend -m "New commit message"

After amend:

# If already pushed (careful!)
git push --force-with-lease origin feature-branch

# If NOT pushed yet:
git push -u origin feature-branch

Amend author:

# Change last commit author
git commit --amend --author="Name <email@example.com>"

# Use configured user
git commit --amend --reset-author

Impact: Clean up last commit without extra commits

When to use:

  • Typo in commit message
  • Forgot to include file
  • Wrong author
  • Before pushing (safe)
  • After pushing own branch (with force-with-lease)

Pattern 4: Reset (Soft, Mixed, Hard)

Problem: Undo commits with different levels

# Three types of reset:
# --soft:  Undo commit, keep staged
# --mixed: Undo commit, unstage (default)
# --hard:  Undo commit, discard changes

Soft reset (keep changes staged):

# Undo last commit, keep changes in staging
git reset --soft HEAD~1

# Before:
# Commit: abc1234 "Add feature"
# Status: Working directory clean

# After:
# Commit: def5678 (previous commit)
# Status: All changes from abc1234 are staged

# Use case: Redo commit with different message
git reset --soft HEAD~1
git commit -m "Better commit message"

Mixed reset (unstage changes):

# Undo last commit, unstage changes (default)
git reset HEAD~1
# Same as:
git reset --mixed HEAD~1

# Before:
# Commit: abc1234 "Add feature"

# After:
# Commit: def5678
# Status: Changes from abc1234 are unstaged

# Use case: Reorganize changes into multiple commits
git reset HEAD~1
git add src/feature-a.js
git commit -m "Add feature A"
git add src/feature-b.js
git commit -m "Add feature B"

Hard reset (discard everything):

# ❌ DANGER: Undo commit AND discard changes
git reset --hard HEAD~1

# Before:
# Commit: abc1234 "Add feature"

# After:
# Commit: def5678
# Status: All changes from abc1234 GONE!

# ⚠️ CANNOT UNDO! (unless using reflog)

Reset to specific commit:

# Reset to specific commit hash
git reset --hard abc1234

# Reset to remote state
git reset --hard origin/main

# Reset to 5 commits ago
git reset --hard HEAD~5

Impact: Undo commits with control over what's kept

When to use:

  • Reorganize commits (soft/mixed)
  • Undo commits on private branch
  • Start over from specific point (hard)
  • Never on public branches!

Pattern 5: Revert (Safe Public Undo)

Problem: Undo commit that's already pushed/shared

# ❌ BAD: Reset public branch
git reset --hard HEAD~1
git push --force origin main  # Breaks others' work!

# ✅ GOOD: Revert (creates new commit)
git revert HEAD

# Creates new commit that undoes changes
# Commit history:
# abc1234 - Add feature
# def5678 - Revert "Add feature"  ← New commit

Revert specific commit:

# Revert by hash
git revert abc1234

# Revert with custom message
git revert abc1234 -m "Revert feature due to bug #123"

# Revert without auto-commit (review first)
git revert abc1234 --no-commit
git status  # Review changes
git commit -m "Revert feature - found security issue"

Revert multiple commits:

# Revert range (inclusive)
git revert abc1234..def5678

# Revert each commit individually
git revert HEAD~3..HEAD

# Revert last 3 commits
git revert HEAD~3 HEAD~2 HEAD~1

Revert merge commit:

# Merge commits have two parents
# Must specify which parent to revert to

# -m 1: Keep first parent (usually main)
git revert -m 1 merge-commit-hash

# Example:
git revert -m 1 HEAD  # Revert last merge

Impact: Safe undo for public history

When to use:

  • Undo commit on main/shared branch
  • Production hotfix (revert bad deploy)
  • Preserve history (audit trail)
  • Safe collaborative undo

Pattern 6: Recover Deleted Commits (Reflog)

Problem: Accidentally deleted commits, need recovery

# ❌ PANIC: "I lost my work!"
git reset --hard HEAD~5  # Deleted 5 commits!
git log  # Commits are gone!

# ✅ SOLUTION: Reflog saves you!
git reflog
# abc1234 HEAD@{0}: reset: moving to HEAD~5
# def5678 HEAD@{1}: commit: Feature work  ← Lost commit!
# ghi9012 HEAD@{2}: commit: More work     ← Lost commit!

# Recover to specific state
git reset --hard HEAD@{1}
# or
git reset --hard def5678

# Commits recovered!

View reflog:

# See all operations
git reflog

# See last 10
git reflog -10

# See specific branch reflog
git reflog show feature-branch

# See with dates
git reflog --date=relative
git reflog --date=iso

Recover deleted branch:

# Deleted branch
git branch -D feature-branch
# Error: branch deleted!

# Find last commit
git reflog | grep feature-branch
# abc1234 HEAD@{5}: checkout: moving from feature-branch to main

# Recreate branch
git branch feature-branch abc1234
git checkout feature-branch
# Branch recovered!

Reflog for specific operations:

# Find when file was deleted
git reflog -- path/to/file.js

# Find all commits
git reflog show --all

# Find specific commit message
git reflog | grep "important feature"

Impact: Recover "lost" commits and branches

When to use:

  • Accidentally reset
  • Deleted branch
  • Lost commits after rebase
  • Any "oops" moment
  • Within 90 days (default reflog expiry)

Pattern 7: Undo Pushed Commits

Problem: Pushed commits that need to be undone

# ❌ BAD: Reset and force push
git reset --hard HEAD~3
git push --force origin main  # Dangerous for team!

# ✅ GOOD: Revert pushed commits
git revert HEAD~3..HEAD
git push origin main

# Or revert individually
git revert HEAD
git revert HEAD~1
git revert HEAD~2
git push origin main

For your own branch:

# If it's your feature branch (not shared)
git reset --hard HEAD~2
git push --force-with-lease origin feature-branch

# Safe because:
# 1. Your branch only
# 2. Not sharing with others
# 3. Using --force-with-lease

For shared branch (proper procedure):

# Step 1: Communicate with team
# "I'm reverting commits abc-def on main"

# Step 2: Revert commits
git revert abc1234..def5678

# Step 3: Push
git push origin main

# Step 4: Tell team to pull
# "Pull latest main, bad commits reverted"

Emergency rollback:

# Production is broken, must rollback NOW

# Option 1: Revert merge
git revert -m 1 HEAD  # Revert last merge
git push origin main

# Option 2: Revert to specific commit
git revert --no-commit HEAD~5..HEAD
git commit -m "Emergency rollback to stable version"
git push origin main

# Option 3: Create tag, then revert
git tag broken-version
git revert -m 1 HEAD
git push origin main
git push origin broken-version  # For investigation later

Impact: Safe undo even after pushing

When to use:

  • Bug found in production
  • Reverting bad merge
  • Undoing public commits
  • Team collaboration

Pattern 8: Reset vs Revert Decision Guide

Problem: Which undo method to use?

Decision tree:

Is commit pushed/shared?
├─ No → Use RESET (clean history)
│   ├─ Want to keep changes? → git reset --soft HEAD~1
│   ├─ Want to unstage? → git reset HEAD~1
│   └─ Want to discard? → git reset --hard HEAD~1
│
└─ Yes → Use REVERT (safe public undo)
    ├─ Single commit → git revert abc1234
    ├─ Multiple commits → git revert abc1234..def5678
    └─ Merge commit → git revert -m 1 merge-hash

Comparison table:

OperationResetRevert
Changes historyYesNo
Creates new commitNoYes
Safe for public branches❌ No✅ Yes
Removes from history✅ Yes❌ No
Undoes specific commit❌ Harder✅ Easy
Can recover with reflog✅ YesN/A

Examples:

# Local experiment failed
git reset --hard HEAD~5  # OK: Not pushed

# Production bug
git revert HEAD  # CORRECT: Already pushed

# Reorganize commits before PR
git reset --soft HEAD~3  # OK: Your branch
git commit  # Combine into one commit

# Undo commit on main
git revert abc1234  # CORRECT: Shared branch

Impact: Choose correct undo method

When to use:

  • Reset: Private branches, local work
  • Revert: Public branches, shared work

Common Workflows

Fix Commit Message Typo

git commit -m "Add authentification"  # Typo!
git commit --amend -m "Add authentication"
git push --force-with-lease origin feature-branch

Reorganize Last 3 Commits

git reset --soft HEAD~3
# All changes now staged
git add src/feature-a.js
git commit -m "Add feature A"
git add src/feature-b.js
git commit -m "Add feature B"
git push --force-with-lease origin feature-branch

Recover Deleted Branch

git branch -D feature-branch  # Oops!
git reflog | grep feature-branch
# Find commit: abc1234
git checkout -b feature-branch abc1234

Revert Production Bug

git checkout main
git pull origin main
git revert abc1234  # Bad commit
git push origin main

Undo Local Uncommitted Changes

# Save first (optional)
git stash

# Discard all changes
git restore .

# Or specific file
git restore src/app.js

Troubleshooting

Amend after push

# Error: Updates were rejected

# If your branch only:
git push --force-with-lease origin feature-branch

# If shared:
# Don't amend! Create new commit instead

Can't find commit in reflog

# Reflog expires after 90 days

# Check if still exists
git fsck --lost-found

# Recover from lost+found
ls .git/lost-found/commit/
git show <hash>
git branch recovered-branch <hash>

Reset wrong commit

# Used reset but meant different commit
git reflog
# Find pre-reset state
git reset --hard HEAD@{1}

Revert created conflict

# Revert may conflict with newer changes
git revert abc1234
# CONFLICT!

# Resolve conflict
git add resolved-file.js
git revert --continue

Best Practices

  1. Check status first - Always git status before major undo
  2. Prefer revert for public - Use revert on shared branches
  3. Reset for private only - Only reset on your own branches
  4. Backup before hard reset - Create branch or stash
  5. Amend before push - OK to amend local commits
  6. Force-with-lease - Safer than --force
  7. Reflog expires - Recover within 90 days
  8. Communicate - Tell team about major undos

Related Sub-Skills

  • Made changes after undo? → /git/basics
  • Need to redo after reset? → /git/history
  • Pushed after undo? → /git/remote
  • Conflicts after revert? → /git/conflicts

Quick Command Reference

# Unstage
git restore --staged file.js         # Unstage file
git restore --staged .                # Unstage all

# Discard changes
git restore file.js                   # Discard file changes
git restore .                         # Discard all changes

# Amend
git commit --amend                    # Change last commit
git commit --amend --no-edit          # Add to last commit

# Reset
git reset --soft HEAD~1               # Undo commit, keep staged
git reset HEAD~1                      # Undo commit, unstage
git reset --hard HEAD~1               # Undo commit, discard

# Revert
git revert HEAD                       # Revert last commit
git revert abc1234                    # Revert specific commit
git revert -m 1 merge-hash            # Revert merge

# Recover
git reflog                            # View reflog
git reset --hard HEAD@{1}             # Recover from reflog
git branch recovered abc1234          # Recover deleted branch

Last Updated: 2025-02-11 Version: 1.0.0

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.