Token导航 LogoToken导航TokenDH.com
研究检索需要联网github未标认证来源可访问clear审计通过

git-recoverygit 恢复

Agent Skill

git-recovery 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

235

周安装

10

GitHub Stars

1

下载量

82
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:git-recovery(git 恢复)
来源仓库:https://github.com/daleseo/git-skills
仓库路径:skills/git-recovery
安装命令:
npx skills add https://github.com/daleseo/git-skills --skill git-recovery
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/daleseo/git-skills --skill git-recovery

简介

主要原则:

  • Git 很少删除 - Reflog 保留 90 天的历史记录
  • 在危险操作之前进行备份 - 分支、标签或存储
  • 对共享分支使用恢复 - 永不重置公共历史记录
  • 销毁前检查 - 试运行、差异、日志
  • 强制租用,而不是强制 - 防止意外覆盖
  • 基本命令:
  • git reflog # 你的安全网
  • git reset --soft HEAD~1 # 撤消提交,保留更改
  • git reset --hard <提交> # 危险!先备份
  • git revert <commit> # 共享分支的安全撤消
  • git switch -c name <hash> # 恢复删除的分支
  • 请记住:几乎所有错误都是可以挽回的。保持冷静,检查引用日志,并在运行之前了解每个命令的作用。
  • 每周安装量
  • 10
  • 存储库
  • daleseo/git 技能
  • GitHub 之星
  • 1
  • 第一次看到
  • 2026 年 1 月 26 日
  • 安全审计
  • Gen Agent Trust Hub 通行证
  • 套接字通行证
  • 斯尼克通行证

SKILL.md

Git Safety and Recovery

Purpose: This skill teaches AI agents to work safely with Git and recover from common mistakes without losing work.

Core Principles

  1. Git Rarely Deletes - Almost everything is recoverable via reflog
  2. Understand Before Destroying - Know what --hard, --force do
  3. Backup Before Risky Operations - Use stash or branches
  4. Revert, Don't Reset - For shared branches

Understanding Git's Safety Net

The Reflog (Your Safety Net)

What is reflog? Git's reflog records every HEAD movement. It's your "undo history" for Git operations.

# ✓ View reflog
git reflog

# Output shows every commit, checkout, reset, rebase, etc:
abc123 HEAD@{0}: commit: feat(auth): add login
def456 HEAD@{1}: checkout: moving from main to feature
ghi789 HEAD@{2}: pull: fast-forward
jkl012 HEAD@{3}: reset: moving to HEAD~1
mno345 HEAD@{4}: commit: fix(bug): temporary fix

How long does reflog keep data?

  • Default: 90 days for reachable commits
  • 30 days for unreachable commits
  • Configurable via gc.reflogExpire

What Can Be Recovered?

# ✓ CAN RECOVER:
- Deleted branches (if deleted recently)
- Reset commits (within 90 days)
- Amended commits (original still in reflog)
- Rebased commits (original still in reflog)
- Dropped stashes (if recent)

# ✗ CANNOT RECOVER:
- Unstaged changes that were never committed
- Untracked files that were never added
- Commits older than gc.reflogExpire
- Files explicitly removed with git clean -f

Common Mistakes and Fixes

Mistake 1: Accidentally Deleted Branch

# ✗ MISTAKE: Deleted wrong branch
git branch -D feature/important-work
# Deleted branch feature/important-work (was abc123).

# ✓ FIX: Recover from reflog
git reflog | grep "important-work"
# abc123 HEAD@{5}: commit: Last commit on important-work

# Recreate branch at that commit
git switch -c feature/important-work abc123

# Or use branch -C to force create
git branch feature/important-work abc123
git switch feature/important-work

Mistake 2: Reset Too Far

# ✗ MISTAKE: Reset and lost commits
git reset --hard HEAD~3
# Now 3 commits are "gone"

# ✓ FIX: Find commits in reflog
git reflog
# abc123 HEAD@{0}: reset: moving to HEAD~3
# def456 HEAD@{1}: commit: feat(auth): important feature  ← Want this!

# Reset back to before the mistake
git reset --hard def456
# Or
git reset --hard HEAD@{1}

Mistake 3: Committed to Wrong Branch

# ✗ MISTAKE: Committed to main instead of feature branch
git switch main
git add src/feature.js
git commit -m "feat(feature): add new feature"
# Oops! This should be on feature branch

# ✓ FIX: Move commit to correct branch

# Create feature branch (includes the commit)
git switch -c feature/new-feature

# Go back to main and remove the commit
git switch main
git reset --hard HEAD~1

# Now:
# - feature/new-feature has the commit
# - main is back to previous state

Mistake 4: Amended Wrong Commit

# ✗ MISTAKE: Amended commit that shouldn't be changed
git commit --amend -m "New message"
# Oops! Original commit was important

# ✓ FIX: Find original commit in reflog
git reflog
# abc123 HEAD@{0}: commit (amend): New message
# def456 HEAD@{1}: commit: Original message  ← Want this!

# Reset to original commit
git reset --hard def456

Mistake 5: Rebased and Lost Commits

# ✗ MISTAKE: Rebase went wrong, some commits disappeared
git rebase -i HEAD~5
# Accidentally dropped important commits

# ✓ FIX: Abort or recover from reflog

# If still in rebase:
git rebase --abort

# If rebase already finished:
git reflog
# Find "rebase (start)" entry
# abc123 HEAD@{10}: rebase (start): checkout main
# def456 HEAD@{11}: commit: Important commit  ← Before rebase started

# Reset to before rebase
git reset --hard def456

Mistake 6: Force Pushed Wrong Branch

# ✗ MISTAKE: Force pushed and overwrote remote commits
git push --force origin main
# Overwrote teammate's commits!

# ✓ FIX: Depends on situation

# If you still have local copy of correct state:
git reflog
git reset --hard <correct-commit>
git push --force-with-lease origin main

# If you don't have it, ask teammate to push their version:
# Teammate runs:
git push --force origin main

# Or restore from remote reflog (if server keeps it):
git fetch origin
git log origin/main@{1}  # One state ago

Mistake 7: Accidentally Removed Staged Changes

# ✗ MISTAKE: Used git restore --staged and lost work
git restore --staged src/important.js
git restore src/important.js  # Oops! Lost changes

# ✓ PREVENTION: Check before restoring
git diff src/important.js  # See what will be lost
git stash push src/important.js  # Safer alternative

# ✗ CANNOT FIX: Unstaged changes cannot be recovered
# Prevention is key!

Reset vs Revert vs Restore

Decision Tree

What do you want to undo?
├─ Local changes not committed yet
│  └─> git restore <file>
│
├─ Staging area (unstage)
│  └─> git restore --staged <file>
│
├─ Last commit (not pushed)
│  ├─ Keep changes in working directory
│  │  └─> git reset --soft HEAD~1
│  │
│  └─ Discard changes completely
│     └─> git reset --hard HEAD~1
│
└─ Commit already pushed
   └─> git revert <commit>  (creates new commit)

Git Reset (For Local Changes)

# Three modes of reset:

# 1. --soft: Move HEAD, keep staged and working directory
git reset --soft HEAD~1
# Use case: Undo commit, keep all changes staged
# Safe: Yes (changes preserved)

# 2. --mixed (default): Move HEAD, unstage, keep working directory
git reset HEAD~1
# OR
git reset --mixed HEAD~1
# Use case: Undo commit, keep changes but unstaged
# Safe: Yes (changes preserved in working directory)

# 3. --hard: Move HEAD, discard staged and working directory
git reset --hard HEAD~1
# Use case: Completely undo commit and discard changes
# Safe: NO (changes lost, but recoverable from reflog)

Reset Examples

# ✓ SAFE: Undo last commit, keep changes
git reset --soft HEAD~1
# Now changes are staged, ready to re-commit differently

# ✓ SAFE: Undo last commit, unstage changes
git reset HEAD~1
# Now changes are in working directory, unstaged

# ✗ DESTRUCTIVE: Undo commit and discard all changes
git reset --hard HEAD~1
# Changes are "gone" (but recoverable from reflog)

# ✓ SAFE: Reset to specific commit
git reset --soft abc123
# All commits after abc123 are undone, changes staged

# ✓ VISUAL: See what would be reset
git log HEAD~3..HEAD  # Shows commits that would be undone
git diff HEAD~3  # Shows changes that would be affected

Git Revert (For Shared Branches)

# ✓ SAFE: Undo commit on shared branch
git revert abc123

# Creates NEW commit that undoes changes:
#   * Revert "feat(auth): add OAuth"  ← New commit
#   * feat(auth): add OAuth           ← Original commit
#   * Previous commit

# Why use revert instead of reset?
# - Doesn't rewrite history
# - Safe for shared branches
# - Preserves audit trail

Revert Examples

# ✓ Revert single commit
git revert abc123
# Opens editor for revert commit message

# ✓ Revert without opening editor
git revert abc123 --no-edit

# ✓ Revert multiple commits
git revert abc123 def456 ghi789

# ✓ Revert range of commits
git revert HEAD~3..HEAD

# ✓ Revert merge commit
git revert -m 1 abc123
# -m 1 means keep first parent (usually main)

Comparison Table

CommandChanges History?Safe for Shared?Recoverable?Use Case
git restoreNoYesNo (working dir changes lost)Undo local file changes
git reset --softYesNoYes (via reflog)Undo commit, keep changes staged
git reset --mixedYesNoYes (via reflog)Undo commit, keep changes unstaged
git reset --hardYesNoYes (via reflog)Undo commit, discard changes
git revertNoYesN/A (creates new commit)Undo commit on shared branch

Recovery Workflows

Workflow 1: Find and Recover Lost Commit

# 1. Search reflog for lost commit
git reflog | grep -i "search term"

# OR view all reflog
git reflog

# 2. Examine commit content
git show abc123

# 3. Recover options:

# Option A: Create new branch at that commit
git switch -c recovered-work abc123

# Option B: Cherry-pick the commit
git cherry-pick abc123

# Option C: Reset current branch to that commit
git reset --hard abc123

Workflow 2: Recover Deleted Branch

# 1. Find branch in reflog
git reflog | grep "branch-name"
# OR
git reflog | grep "checkout"

# 2. Find last commit on that branch
# abc123 HEAD@{5}: commit: Last commit on branch-name

# 3. Recreate branch
git switch -c branch-name abc123

# 4. Verify
git log

Workflow 3: Undo Bad Merge

# Just merged, but it was wrong

# ✓ OPTION 1: Reset (if not pushed)
git reset --hard HEAD~1

# ✓ OPTION 2: Revert (if already pushed)
git revert -m 1 HEAD
# -m 1 keeps the main branch parent

# ✓ OPTION 3: Abort (if still in merge)
git merge --abort

Workflow 4: Recover from Bad Rebase

# Rebase went wrong

# ✓ OPTION 1: Abort (if still in rebase)
git rebase --abort

# ✓ OPTION 2: Recover from reflog (if already finished)
git reflog
# Find entry before rebase started:
# abc123 HEAD@{10}: checkout: moving from feature to main
# def456 HEAD@{11}: commit: Last commit before rebase

git reset --hard def456

Workflow 5: Recover Dropped Stash

# Accidentally dropped stash

# 1. Find stash in reflog
git reflog | grep stash
# abc123 WIP on main: Previous stash
# def456 index on main: Another stash

# 2. Create branch from stash commit
git switch -c recovered-stash abc123

# 3. Apply the stash
git stash apply abc123

# OR directly apply from reflog
git stash apply abc123

Preventive Measures

Before Risky Operations

# ✓ BACKUP 1: Create safety branch
git switch -c backup-before-rebase
git switch feature-branch
# Do risky operation
# If successful, delete backup:
git branch -d backup-before-rebase

# ✓ BACKUP 2: Use stash
git stash push -m "Backup before risky operation"
# Do risky operation
# If successful:
git stash drop
# If failed:
git stash pop

# ✓ BACKUP 3: Tag current state
git tag backup-$(date +%Y%m%d-%H%M%S)
# Do risky operation
# If successful:
git tag -d backup-20240115-143000

Configure Safety Settings

# ✓ Require confirmation for destructive operations
git config --global alias.yolo '!echo "Are you sure? Use git reset --hard if certain"'

# ✓ Increase reflog retention
git config --global gc.reflogExpire 180  # 180 days instead of 90
git config --global gc.reflogExpireUnreachable 60  # 60 days instead of 30

# ✓ Always use --force-with-lease
git config --global alias.pushf 'push --force-with-lease'

# ✓ Protect against accidental git clean
git config --global clean.requireForce true

Pre-operation Checklist

Before running destructive commands:

  • Do I have a backup? (branch, tag, or stash)
  • Is this branch pushed to remote? (additional backup)
  • Have I verified what will be changed? (git diff, git log)
  • Do I understand what this command does?
  • Can I recover using reflog if something goes wrong?

Advanced Recovery

Recover Specific Files from History

# ✓ Restore file from specific commit
git restore --source=abc123 src/important.js

# ✓ Restore file from before it was deleted
git log --all --full-history -- src/deleted-file.js
# Find commit where it existed
git restore --source=abc123 src/deleted-file.js

# ✓ Restore file from stash
git show stash@{0}:src/file.js > src/file.js

Recover from git clean

# ✗ PROBLEM: Ran git clean and deleted untracked files
git clean -fd
# Deleted important-file.js (untracked)

# ✓ LIMITED RECOVERY OPTIONS:

# Option 1: Check editor auto-save
# Many editors keep unsaved file backups

# Option 2: Check OS trash/recycle bin
# Some Git GUIs move files to trash instead of deleting

# Option 3: File recovery tools
# Use OS-level file recovery (PhotoRec, TestDisk, etc.)

# ✗ PREVENTION: Always use -n first
git clean -nfd  # -n = dry run, shows what would be deleted
# Review output
git clean -fd   # Actually delete

Recover from Rewritten History

# Someone force-pushed and rewrote history

# ✓ Find old commits in local reflog
git reflog
git log --all --oneline

# ✓ Recovery options:

# Option 1: Push your version (if yours is correct)
git push --force-with-lease origin main

# Option 2: Merge both versions
git fetch origin
git merge origin/main
# Resolve conflicts
git push

# Option 3: Cherry-pick missing commits
git cherry-pick abc123 def456
git push

Common Dangerous Commands

Commands to Use Carefully

# ✗ DANGEROUS: Can lose work

git reset --hard <commit>
# Discards all changes in working directory
# Prevention: git stash first

git clean -fd
# Deletes untracked files permanently
# Prevention: git clean -nfd first (dry run)

git push --force
# Overwrites remote, can lose others' work
# Prevention: Use --force-with-lease instead

git rebase -i
# Can drop/edit commits, complex conflicts
# Prevention: Create backup branch first

git branch -D <branch>
# Deletes branch even if not merged
# Prevention: Use -d first (safer), recover from reflog if needed

Safe Alternatives

# ✓ SAFER ALTERNATIVES:

# Instead of: git reset --hard
git stash  # Or git stash push -u to include untracked

# Instead of: git push --force
git push --force-with-lease

# Instead of: git clean -fd
git clean -nfd  # Dry run first
git stash push -u  # Stash untracked files

# Instead of: git branch -D
git branch -d  # Refuses if not merged
git merge --no-ff <branch>  # Merge first, then delete

# Instead of: git reset --hard HEAD~3
git reset --soft HEAD~3  # Keep changes
git restore --staged .   # Unstage if needed

Quick Reference

Recovery Commands

# View reflog
git reflog
git reflog show <branch>

# Recover deleted branch
git switch -c <branch> <commit-hash>

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1
git reflog  # Can still recover

# Revert commit (safe for shared branches)
git revert <commit>

# Recover from bad merge
git reset --hard HEAD~1  # If not pushed
git revert -m 1 HEAD     # If pushed

# Find deleted file
git log --all --full-history -- <file>
git restore --source=<commit> <file>

# Recover dropped stash
git reflog | grep stash
git stash apply <stash-hash>

Summary

Key Principles:

  1. Git rarely deletes - Reflog keeps 90 days of history
  2. Backup before risky operations - Branch, tag, or stash
  3. Use revert for shared branches - Never reset public history
  4. Check before destroying - Dry runs, diffs, logs
  5. Force-with-lease, not force - Prevents accidental overwrites

Essential Commands:

git reflog                    # Your safety net
git reset --soft HEAD~1       # Undo commit, keep changes
git reset --hard <commit>     # Dangerous! Backup first
git revert <commit>           # Safe undo for shared branches
git switch -c name <hash>     # Recover deleted branch

Remember: Almost every mistake is recoverable. Stay calm, check reflog, and understand what each command does before running it.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

能力 5

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude Code

25.71%
按下载量换算21

OpenCode

21.51%
按下载量换算18

Cursor

16.14%
按下载量换算13

Codex

13.27%
按下载量换算11

goose

8.01%
按下载量换算7

github-copilot

3.64%
按下载量换算3

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

来源信息

继续浏览同类 Skills