agentleFS
28672032962867203296/hermes-agent/skills/software-development/git-rescue/SKILL.md

git-rescue

Skill0 starsChanged 3 months agoMITView on GitHub

Git救急手册:撤销、回滚、找回丢失提交、解决冲突.

  • Deletes or force-pushes
  • Commits and pushes

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Git 救急手册

借鉴 k88hudson/git-flight-rules (43k⭐)。

我搞砸了,怎么办?

改错了还没 commit

git checkout -- <file>      # 撤销单个文件
git checkout -- .            # 撤销所有修改
git restore <file>           # Git 2.23+ 推荐

commit 了但还没 push

git reset HEAD~1             # 撤销最新commit,改动保留
git reset --hard HEAD~1      # 撤销最新commit,改动也删
git commit --amend           # 修改最新commit信息
git commit --amend --no-edit # 追加改动到最新commit

已经 push 了

git revert <commit>          # 创建新commit来撤销(安全)
git push origin main         # push 撤销commit

我在错误的分支上干活了

git stash                    # 暂存当前改动
git checkout correct-branch
git stash pop                # 恢复改动到正确分支

我 rebase 搞乱了一切

git reflog                   # 查看所有操作历史
git reset --hard HEAD@{2}    # 回到rebase前的状态
# reflog 会保留30天的操作记录

我 merge 错了分支

git reset --hard ORIG_HEAD   # 回到merge前(merge后立即用)
git reflog                   # 如果已经做了其他操作

找不回被删除的提交

git reflog                   # 找到删除前的commit hash
git checkout <hash>           # 切到那个commit
git checkout -b rescue-branch # 基于它建新分支
git cherry-pick <hash>        # 或把那个commit捡回来

提交规范补救

把多个小commit合成一个

git rebase -i HEAD~3         # 交互式rebase最近3个commit
# 在编辑器里把除第一个外的 pick 改成 squash

把一个commit拆成多个

git rebase -i HEAD~3
# 把要拆的 commit 标为 edit
git reset HEAD~1
git add file1.py && git commit -m "part 1"
git add file2.py && git commit -m "part 2"
git rebase --continue

改很久以前的commit信息

git rebase -i HEAD~5
# 把要改的标为 reword,保存后编辑器会弹出让你改

分支操作

基于远程分支创建

git fetch origin
git checkout -b feature origin/main

把一个分支的改动移到另一个分支

git log feature-branch --oneline   # 找到要移的commit
git checkout target-branch
git cherry-pick <commit-hash>

删除远程分支

git push origin --delete old-branch

查看历史

git log --oneline --graph --all     # 图形化历史
git log -p <file>                   # 看某个文件的变更历史
git blame <file>                    # 看每行是谁改的
git diff main..feature              # 两个分支的差异
git show <commit>                   # 看某个commit的详情

黄金法则

  1. push 前什么都好办(reset/amend/stash)
  2. push 后用 revert(不要 reset --hard 已经 push 的)
  3. 遇到任何问题先 git status(看清楚当前状态)
  4. 不确定就先建备份分支(git checkout -b backup)
  5. reflog 是救命稻草(30天内所有操作都有记录)

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.