Git Recovery

VerifiedCaution

Quickly fix common git mistakes: undo commits, recover deleted files, resolve conflicts, and fix detached HEAD.

Sby Skills Guide Bot
DevOpsIntermediate
7/24/2026
Claude CodeCursorWindsurfCopilotCodex
#git#version-control#recovery#merge-conflicts#troubleshooting

Recommended for

Automated summary

A Git troubleshooting playbook that supplies the commands to undo commits, restore deleted files, relocate a commit made on the wrong branch, and resolve or abort a conflicted merge.

Strengths

  • Covers the most common mistakes: reset --soft/--hard, files deleted before or after a commit, committing to the wrong branch, detached HEAD
  • Each scenario ships with exact commands and their effect, ready to copy and paste
  • Points to git reflog as the safety net after a destructive reset --hard
  • Clearly separates already-pushed cases (git revert) from local-only ones (git reset)

Limitations

  • Diagnosis is left to the user; the skill does not inspect repository state to identify the problem
  • Does not address rewriting history on a shared branch beyond git revert, nor interactive rebase
  • The provided document cuts off mid-section on conflicts, so bulk resolution coverage is incomplete
When to use it

Reach for it whenever a Git command went wrong and you need to undo, restore, or unblock a repository quickly.

When not to use it

Skip it when learning Git from scratch or performing routine error-free operations like a normal commit or push.

Security analysis

Caution
Quality score88/100

The skill provides legitimate git recovery techniques but includes commands that can cause irreversible data loss (e.g., reset --hard, clean). It is not malicious, but the power of these commands warrants caution. No network or exfiltration risks are present.

Findings
  • •Uses powerful git commands like `git reset --hard`, `git clean -fd`, and `git push --force` which can permanently delete uncommitted work or rewrite history.
  • •Instructions for discarding changes and cleaning untracked files could lead to data loss if followed incorrectly.
  • •No explicit confirmation steps or safety checks before running destructive commands beyond textual warnings.
How this analysis is made

Examples

Commit on the wrong branch
I accidentally committed to main instead of my feature/login branch and haven't pushed yet — how do I move that commit over?
Recover a committed deletion
I deleted src/config.ts by accident and the deletion is already committed. Can I get the file back?
Abort a messy merge
My merge has conflicts in three files and I just want to back out and start over — what do I run?

name: git-recovery description: Recovers from git mistakes including undoing commits, recovering deleted files, resolving conflicts, and fixing detached HEAD. Use when user says "undo commit", "recover file", "git reset", "git mistake", "deleted by accident", "merge conflict", or mentions git errors. metadata: author: Mastering Claude Code version: 1.0.0 category: emergency

Git Recovery

Quickly fix common git mistakes and recover lost work.

Instructions

Step 1: Identify What Went Wrong

Common git mistakes:

  • Committed to wrong branch (committed to main instead of feature branch)
  • Want to undo last commit(s)
  • Accidentally deleted a file
  • Merge conflict
  • Detached HEAD state
  • Pushed something that shouldn't have been pushed
  • Lost changes after bad merge/rebase

Step 2: Apply the Appropriate Fix

Undo Last Commit (Keep Changes)

Most common scenario: "I just committed but forgot to add a file"

# Undo commit, keep changes staged
git reset --soft HEAD~1

# Now add the forgotten file
git add forgotten-file.txt

# Commit again
git commit -m "Your message"

Keep changes unstaged:

# Undo commit, keep changes but unstaged
git reset HEAD~1

# Now you can modify files before committing again

Undo Last Commit (Discard Changes)

Warning: This deletes your work. Only use if you're sure.

# Undo commit and delete all changes
git reset --hard HEAD~1

Undo last 3 commits:

git reset --hard HEAD~3

If you regret this:

# Find your commit in reflog
git reflog

# Recover it (find the commit hash from reflog)
git reset --hard [COMMIT_HASH]

Recover Deleted File

File deleted but NOT committed:

# Recover one file
git checkout -- path/to/deleted-file.txt

# Or with newer git:
git restore path/to/deleted-file.txt

File deleted AND committed:

# Find when it was deleted
git log --all --full-history -- path/to/file.txt

# Restore from commit before deletion
git checkout [COMMIT_HASH]~1 -- path/to/file.txt

# Now commit the recovered file
git add path/to/file.txt
git commit -m "Recover deleted file"

Can't remember the filename:

# Search for deleted files
git log --diff-filter=D --summary | grep delete

# Or see all deleted files in last 10 commits
git log --diff-filter=D --name-only --pretty=format: HEAD~10..HEAD

Fix: Committed to Wrong Branch

Scenario: You committed to main but should have committed to feature/login

If you haven't pushed yet:

# 1. Note your commit hash
git log -1
# Copy the commit hash

# 2. Undo the commit on main (keep changes)
git reset --soft HEAD~1

# 3. Switch to correct branch (create if needed)
git checkout -b feature/login

# 4. Commit there
git commit -m "Your commit message"

# 5. Go back to main
git checkout main

If you already pushed to main:

# This is trickier - you need to revert on main

# 1. Create the feature branch from current main
git checkout -b feature/login

# 2. Go back to main
git checkout main

# 3. Revert the commit (this creates a new "undo" commit)
git revert HEAD
git push

Resolve Merge Conflicts

When you see:

CONFLICT (content): Merge conflict in src/App.tsx
Automatic merge failed; fix conflicts and then commit the result.

Fix process:

# 1. See which files have conflicts
git status

# 2. Open the conflicted file
# Look for conflict markers:
# <<<<<<< HEAD
# Your changes
# =======
# Their changes
# >>>>>>> branch-name

# 3. Edit the file, choose which changes to keep
# Remove the <<<<<<< ======= >>>>>>> markers

# 4. Mark as resolved
git add path/to/resolved-file.txt

# 5. Complete the merge
git commit

Abort a merge:

# If you want to start over
git merge --abort

Accept "theirs" for all conflicts:

git checkout --theirs .
git add .
git commit

Accept "ours" for all conflicts:

git checkout --ours .
git add .
git commit

Fix Detached HEAD

You'll see: HEAD detached at [commit]

What happened: You checked out a specific commit instead of a branch.

Fix:

# Option 1: Create a branch from here
git checkout -b new-branch-name

# Option 2: Go back to a branch
git checkout main

# If you made commits in detached HEAD:
# 1. Note the commit hash
git log -1
# 2. Go to your branch
git checkout main
# 3. Cherry-pick those commits
git cherry-pick [COMMIT_HASH]

Undo a Push (Rewrite History)

Warning: Only do this if you're the only one using the branch.

# Undo last commit locally
git reset --hard HEAD~1

# Force push to remote
git push --force origin [BRANCH_NAME]

Safer alternative (doesn't rewrite history):

# Create a "reverse" commit
git revert HEAD
git push

Recover Lost Commits

Scenario: You did git reset --hard and lost work.

# 1. View reflog (shows all HEAD movements)
git reflog

# Output looks like:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# e4f5g6h HEAD@{1}: commit: Add feature
# i7j8k9l HEAD@{2}: commit: Fix bug

# 2. Find the commit you want to recover
# 3. Reset to that commit
git reset --hard e4f5g6h

Reflog is your time machine - it keeps commits for ~90 days.


Unstage Files

You added files you don't want to commit:

# Unstage one file
git reset HEAD path/to/file.txt

# Unstage all files
git reset HEAD

Discard All Local Changes

Start over from last commit:

# Discard all uncommitted changes
git reset --hard HEAD

# Also delete untracked files
git clean -fd

Common Scenarios

Scenario 1: "I committed to main by mistake"

User says: "I just committed to main but I should have created a feature branch"

Response:

# 1. Create the feature branch (keeps your commit)
git checkout -b feature/my-work

# 2. Go back to main
git checkout main

# 3. Reset main to before your commit
git reset --hard origin/main

# 4. Go back to your feature branch
git checkout feature/my-work

# Your commit is now on feature/my-work, main is clean

Scenario 2: "I want to undo my last commit"

User says: "I need to undo my last commit but keep the changes"

Response:

# Undo commit, keep changes staged
git reset --soft HEAD~1

# Now modify files if needed, then commit again
git commit -m "Fixed commit message"

Scenario 3: "I accidentally deleted a file"

User says: "I deleted src/components/Header.tsx by accident and need it back"

If NOT committed:

git checkout -- src/components/Header.tsx

If already committed:

# Find the commit where you deleted it
git log --all -- src/components/Header.tsx

# Restore from one commit before deletion
git checkout [COMMIT_HASH]~1 -- src/components/Header.tsx

# Commit the recovery
git add src/components/Header.tsx
git commit -m "Recover deleted Header.tsx"

Scenario 4: "Merge conflict - how do I fix this?"

User says: "I tried to merge and got conflicts in App.tsx"

Response:

# 1. Check which files have conflicts
git status

# 2. Open App.tsx, look for these markers:
# <<<<<<< HEAD
# Your changes
# =======  
# Their changes
# >>>>>>> branch-name

# 3. Edit the file: keep what you want, delete the markers

# 4. Mark as resolved
git add App.tsx

# 5. Complete the merge
git commit

# If you want to abort instead:
git merge --abort

Scenario 5: "I'm in detached HEAD state"

User says: "Git says 'HEAD detached at abc123'"

Response:

# If you want to keep your changes here:
git checkout -b new-branch-name

# If you want to go back to main:
git checkout main

# Your work isn't lost - it's in reflog for 90 days

Quick Reference

| Problem | Quick Fix | |---------|-----------| | Undo last commit (keep changes) | git reset --soft HEAD~1 | | Undo last commit (discard) | git reset --hard HEAD~1 | | Recover deleted file (uncommitted) | git checkout -- [FILE] | | Recover deleted file (committed) | git checkout [HASH]~1 -- [FILE] | | Committed to wrong branch | Create branch, reset original | | Merge conflict | Edit file, remove markers, git add, git commit | | Detached HEAD | git checkout -b new-branch or git checkout main | | Unstage file | git reset HEAD [FILE] | | Discard all changes | git reset --hard HEAD | | Recover lost commits | git reflog then git reset --hard [HASH] |


Prevention Tips

Before committing:

# Always check what you're committing
git status
git diff

# Check which branch you're on
git branch

Before pushing:

# Review commits
git log --oneline -5

# Make sure you're on the right branch
git branch

Use feature branches:

# Never work directly on main
git checkout -b feature/my-work

# Do your work, commit, then merge via PR

Commit frequently:

# Small commits are easier to undo
# Instead of one big commit, do:
git add src/Header.tsx
git commit -m "Add Header component"

git add src/Footer.tsx
git commit -m "Add Footer component"

When to Use This Skill

✅ Use git-recovery when:

  • Made a git mistake
  • Need to undo commits
  • Deleted a file by accident
  • Merge conflict
  • Detached HEAD
  • Committed to wrong branch
  • Lost work after bad git operation

❌ Don't use git-recovery for:

  • Learning git fundamentals (read GIT_FOR_VIBE_CODERS.md guide)
  • Setting up git workflow (that's architecture)
  • Advanced git operations (rebase, submodules, etc.)
  • Git configuration

This skill fixes git mistakes. For learning git workflows, see the GIT_FOR_VIBE_CODERS guide in your framework.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/git-recovery-mryiqi0y/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/git-recovery-mryiqi0y' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/git-recovery-mryiqi0y/download' --output '.skill-downloads/git-recovery-mryiqi0y/SKILL.md.part' && mv '.skill-downloads/git-recovery-mryiqi0y/SKILL.md.part' '.skill-downloads/git-recovery-mryiqi0y/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

Original excerpt: Quick Reference
ProblemQuick Fix
Undo last commit (keep changes)git reset --soft HEAD~1
Undo last commit (discard)git reset --hard HEAD~1
Recover deleted file (uncommitted)git checkout -- [FILE]
Recover deleted file (committed)git checkout [HASH]~1 -- [FILE]
Committed to wrong branchCreate branch, reset original
Merge conflictEdit file, remove markers, git add, git commit
Detached HEADgit checkout -b new-branch or git checkout main
Unstage filegit reset HEAD [FILE]
Discard all changesgit reset --hard HEAD
Recover lost commitsgit reflog then git reset --hard [HASH]

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/git-recovery-mryiqi0y/SKILL.md' && test -s '.skill-downloads/git-recovery-mryiqi0y/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/git-recovery' && cp -R '.skill-downloads/git-recovery-mryiqi0y' '.claude/skills/git-recovery'

Cursor
test -f '.skill-downloads/git-recovery-mryiqi0y/SKILL.md' && test -s '.skill-downloads/git-recovery-mryiqi0y/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/git-recovery' && cp -R '.skill-downloads/git-recovery-mryiqi0y' '.cursor/skills/git-recovery'

Codex
test -f '.skill-downloads/git-recovery-mryiqi0y/SKILL.md' && test -s '.skill-downloads/git-recovery-mryiqi0y/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/git-recovery' && cp -R '.skill-downloads/git-recovery-mryiqi0y' '.agents/skills/git-recovery'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/git-recovery-mryiqi0y'

openskills read 'git-recovery-mryiqi0y'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/git-recovery-mryiqi0y/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills