agentleFS
pvnarppvnarp/agent-skills/skills/git-recovery/SKILL.md

git-recovery

Skill1 starsChanged 9 months agoMITView on GitHub

Recovers from git disasters - force-pushed over work, broken rebase, detached HEAD, lost commits, bad merges, and accidentally deleted branches. Use when git state is broken and you need to recover work without making things worse.

  • Deletes or force-pushes
  • Commits and pushes

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    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
  1. Distinctive:

    Stop. Don't run another command until you've diagnosed the situation.

    line 8
  2. Distinctive:

    Every entry is a commit SHA you can recover.

    line 20
  3. Distinctive:

    If someone else already pulled the bad state, coordinate with them before force pushing again.

    line 37
  4. Distinctive:

    Before running any of these: git stash your uncommitted work, and note the current SHA from git log --oneline -1.

    line 185

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “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 Recovery

Stop. Don't run another command until you've diagnosed the situation. Most git "disasters" are fully recoverable if you don't make them worse.

First: Check the Reflog

The reflog records every HEAD movement for the last 90 days. Even "deleted" commits are still there.

git reflog                    # See recent HEAD positions
git reflog show main          # See recent main branch positions
git reflog show --all         # All branches

Every entry is a commit SHA you can recover. The reflog is your safety net.

Common Disasters

"I force-pushed and lost commits on remote"

# 1. Find the commit before the force push in reflog
git reflog

# 2. The old HEAD is still there. Reset to it.
git reset --hard <old-sha>

# 3. Force push the recovered state
git push --force-with-lease origin branch-name

If someone else already pulled the bad state, coordinate with them before force pushing again.

"I'm in detached HEAD state"

# You're not on a branch. Your commits are floating.

# If you have commits to save:
git branch recover-branch     # Create a branch at current position
git checkout main             # Go back to a real branch
git merge recover-branch      # Merge your work in

# If you just checked out a tag/SHA by accident:
git checkout main             # Just go back to your branch

"I rebased and everything is broken"

# 1. Find the pre-rebase state in reflog
git reflog

# 2. Look for the entry BEFORE the rebase started (e.g., "rebase started")
# It will look like: abc1234 HEAD@{5}: checkout: moving from feature to main

# 3. Reset to the pre-rebase state
git reset --hard <pre-rebase-sha>

During a rebase that's going badly:

git rebase --abort    # Undo the entire rebase, go back to before

"I committed to the wrong branch"

# Committed to main when you meant to commit to feature-branch

# 1. Note the commit SHA
git log --oneline -3

# 2. Create/switch to the right branch with the commit
git branch feature-branch     # Branch now has the commit too
# or if branch exists:
git checkout feature-branch
git cherry-pick <sha>

# 3. Remove the commit from the wrong branch
git checkout main
git reset --hard HEAD~1       # Remove last commit from main

"I accidentally deleted a branch"

# 1. Find the branch tip in reflog
git reflog | grep "branch-name"
# or
git reflog --all | grep "branch-name"

# 2. Recreate the branch at that commit
git branch branch-name <sha>

"I ran git reset --hard and lost uncommitted work"

If the files were ever staged (git add):

# Staged files are stored as blobs
git fsck --lost-found
# Look in .git/lost-found/other/ for your files

If the files were never staged: They're gone. This is the one truly unrecoverable git scenario. This is why you commit early and often.

"Merge conflict and I don't know what to do"

# See what's conflicted
git status

# For each conflicted file, edit and resolve the markers:
# <<<<<<< HEAD
# your changes
# =======
# their changes
# >>>>>>> branch-name

# After resolving:
git add resolved-file.txt
git commit                    # Complete the merge

# If you want to abort the entire merge:
git merge --abort

"I need to undo the last commit but keep the changes"

git reset --soft HEAD~1       # Undo commit, keep changes staged
git reset HEAD~1              # Undo commit, keep changes unstaged
git reset --hard HEAD~1       # Undo commit AND discard changes (careful!)

"I need to undo a commit that's already pushed"

# Create a NEW commit that reverses the bad one (safe for shared branches)
git revert <sha>
git push

# NEVER use reset + force push on shared branches unless coordinating with the team

Recovery Decision Tree

Is the commit in the reflog?
  YES → git reset --hard <sha> or git cherry-pick <sha>
  NO  → Was it ever staged?
    YES → git fsck --lost-found
    NO  → Was it ever committed?
      YES → Check reflog again (git reflog --all)
      NO  → The work is gone. Sorry.

Prevention

PracticePrevents
Commit early, commit oftenLosing unstaged work
--force-with-lease not --forceOverwriting others' pushed commits
Branch protection rulesAccidental force push to main
git stash before risky operationsLosing uncommitted work during rebase/reset
Don't rewrite shared historyConfusion and lost work across the team

Dangerous Commands (Require Caution)

git reset --hard      # Discards uncommitted changes permanently
git push --force      # Overwrites remote history (use --force-with-lease)
git clean -fd         # Deletes untracked files permanently
git checkout -- .     # Discards all unstaged changes
git rebase -i         # Rewrites history (can lose commits if done wrong)

Before running any of these: git stash your uncommitted work, and note the current SHA from git log --oneline -1.

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.