| name | git-troubleshooter |
| description | Git常见问题排查与修复,包括冲突处理、回退、清理、cherry-pick、reflog恢复等疑难杂症 |
| triggers | ["git冲突","git回退","git错误","git恢复","git reset","git rebase失败","git分支乱了"] |
git-troubleshooter — Git故障排查员
Git出问题了别慌!这个skill帮你诊断并修复所有常见Git疑难杂症。
触发条件
- Git操作报错(冲突、失败、拒绝等)
- 需要回退到之前的版本
- 分支混乱,需要清理
- 误删内容想要恢复
工具列表
主要使用 exec 工具执行Git命令
常见问题急救手册
1. 合并冲突(Merge Conflict)
识别:
<<<<<<< HEAD
当前分支的内容
=======
incoming分支的内容
>>>>>>> feature-branch
解决方法(三步走):
步骤1:查看冲突文件
git status
git diff --name-only --diff-filter=U
步骤2:手动解决冲突
编辑冲突文件,保留需要的部分,删除 <<<<<<<、=======、>>>>>>> 标记
步骤3:标记解决完成
git add <冲突文件>
git commit -m "fix: resolve merge conflict in XXX"
快捷工具:
git checkout --ours <file>
git checkout --theirs <file>
git merge -X ours <branch>
git merge -X theirs <branch>
2. 想回退(Reset/Revert)
场景A:还没push,想重置本地
git reset --soft HEAD~1
git reset --hard HEAD~1
git reset --hard <commit-hash>
场景B:已经push了,想撤销
git revert <commit-hash>
git revert HEAD
场景C:想回到某个tag
git reset --hard v1.0.0
git push --force origin <branch>
3. 误删了分支,想恢复
git reflog
git checkout -b <分支名> <commit-hash>
git checkout -b feature-x def5678
或者用 HEAD@{n} 直接恢复:
git checkout -b recovered-branch HEAD@{5}
4. Rebase冲突中断
停在了冲突点:
git add .
git rebase --continue
git rebase --abort
已经手动reset了?用reflog恢复:
git reflog | grep rebase
git reset --hard ORIG_HEAD
5. 修改了不想提交的改动,想stash
git stash
git stash save "临时stash: XXX改动"
git stash list
git stash pop
git stash apply stash@{0}
git stash drop stash@{0}
6. 想撤销某次文件的修改
git checkout -- <file>
git restore <file>
git checkout -- .
git restore .
7. push被拒绝(non-fast-forward)
原因:远程有更新的提交
解决方法:
git pull --rebase origin <branch>
git push origin <branch>
git push --force origin <branch>
8. 查看历史记录
git log --oneline -10
git log --graph --oneline --all
git log -S "关键词" --oneline
git log -p <file>
git blame <file>
危险操作警告
| 操作 | 危险等级 | 恢复方式 |
|---|
git reset --hard | 🔴 高 | git reflog 恢复 |
git push --force | 🔴 高 | git reflog + 远程恢复 |
git checkout . | 🟡 中 | 无法恢复 |
git clean -fd | 🔴 高 | 无法恢复(做好备份) |
最佳实践:危险操作前先 git branch backup 创建备份分支