Claude-skill-registry fix-git
This skill provides guidance for recovering lost Git commits, resolving detached HEAD states, and fixing common Git repository issues. Use this skill when encountering lost commits, accidental checkouts, orphaned branches, merge conflicts during recovery, or needing to restore repository state from reflog history.
git clone https://github.com/majiayu000/claude-skill-registry
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/fix-git" ~/.claude/skills/majiayu000-claude-skill-registry-fix-git && rm -rf "$T"
skills/data/fix-git/SKILL.mdGit Recovery and Repair
This skill provides systematic approaches for diagnosing and recovering from common Git repository issues, particularly lost commits, detached HEAD states, and merge conflicts during recovery operations.
When to Use This Skill
- Recovering lost commits after accidental operations (reset, checkout, rebase)
- Fixing detached HEAD states where commits were made outside any branch
- Restoring work after improper branch operations
- Resolving merge conflicts during recovery merges
- Diagnosing repository state to understand what went wrong
Diagnostic Approach
Initial Assessment
Before attempting any recovery, gather comprehensive repository state information:
- Check current state: Run
,git status
, andgit branch -a
to understand the current positiongit log --oneline -10 - Examine reflog: Run
to see the complete history of HEAD movements, including operations that don't appear in normal git loggit reflog - Identify the problem: Determine whether dealing with a detached HEAD, lost commits, or branch confusion
Understanding Reflog Output
The reflog shows every HEAD position change with commit hashes and operation descriptions. Key patterns to identify:
indicates entering a detached HEAD statecheckout: moving from X to HEAD~N
entries in detached HEAD state represent orphaned commits at risk of garbage collectioncommit:
indicates leaving detached HEAD state (potentially abandoning commits)checkout: moving from <hash> to <branch>
Recovery Strategies
Lost Commits from Detached HEAD
When commits were made in a detached HEAD state and then lost:
- Use
to find the commit hash containing the lost workgit reflog - Verify the commit contents with
orgit show <hash>git log -1 -p <hash> - Decide on recovery method:
- Merge: Preserves full history, appropriate when commits should be part of branch history
- Cherry-pick: Applies changes without merge commit, appropriate for selective recovery
- Create branch: Run
to preserve commits for later decisiongit branch recovery-branch <hash>
Merge-Based Recovery
When merging recovered commits back into a branch:
git checkout <target-branch> git merge <recovered-commit-hash> -m "Recover lost changes from <description>"
Handling Merge Conflicts During Recovery
Merge conflicts during recovery require careful resolution:
- Identify all conflicting files: Check
for the complete list of unmerged filesgit status - Understand both versions: Before resolving, read the conflict markers to understand what each version contains
- Resolve with intention: Determine which version should prevail based on the recovery goal
- Remove all conflict markers: Ensure no
,<<<<<<<
, or=======
markers remain>>>>>>> - Verify resolution: After editing, read the resolved file to confirm correctness
Verification Protocol
Post-Recovery Verification (Critical)
After any recovery operation, perform these verification steps:
- Check merge/operation status: Run
to confirm no unfinished operationsgit status - Verify file contents: Read recovered files to ensure no conflict markers remain and content is correct
- Review commit history: Run
to confirm the recovery commit appears correctlygit log --oneline -5 - Check all affected files: If multiple files were involved, verify each one merged cleanly
- Test functionality: If applicable, run tests or manually verify the recovered code works
Conflict Marker Detection
After resolving conflicts, search for residual markers:
grep -r "<<<<<<" <file> grep -r "======" <file> grep -r ">>>>>>" <file>
Common Pitfalls
Mistakes to Avoid
- Incomplete conflict resolution: Leaving partial conflict markers (e.g., only removing
but not<<<<<<<
)>>>>>>> - Ignoring secondary files: When a commit touches multiple files, focusing only on the conflicting file and ignoring others
- No pre-recovery backup: Not creating a safety branch before attempting recovery merges
- Assuming success without verification: Not reading the final file content after conflict resolution
- Skipping reflog analysis: Attempting recovery without fully understanding the sequence of operations that caused the issue
User Communication
When helping users recover from Git issues:
- Explain the root cause: Help users understand what operation caused the problem (e.g., "Checking out HEAD~1 put you in a detached HEAD state")
- Confirm conflict resolution choices: When resolving conflicts with significant content differences, verify which version the user wants
- Educate on prevention: Briefly explain how to avoid the issue in the future
Safety Measures
Before Recovery Operations
- Create a backup branch:
preserves the current stategit branch backup-before-recovery - Note current commit: Record the current HEAD hash in case rollback is needed
- Check for uncommitted changes: Ensure working directory is clean before recovery operations
Recovery Rollback
If a recovery operation produces incorrect results:
git reset --hard <backup-commit-hash>
Or if a backup branch was created:
git reset --hard backup-before-recovery
Reference
For detailed recovery scenarios and advanced techniques, see
references/git_recovery_guide.md.