🌿 Git 与版本控制问题
合并冲突、Rebase 丢提交、Force push 覆盖队友、detached HEAD、LFS、Submodule、大文件、CRLF。
Git 的报错信息少有"明显错误",更多是"我看不懂自己的状态"——commit 不见了、teammate 的工作被 force push 覆盖、Submodule 怎么都拉不下来、大文件让 push 失败、CRLF 一次性炸出 3000 行 diff。 这个 Hub 按真实事故场景分条:每篇文章一种症状(不是"git 教程"),给出"先 git reflog 看一眼 / 用 git fsck 找悬空对象 / 用 git filter-repo 清掉大文件"这类可执行修复步骤。 与 AI 编程工具的 [[ai-coding-issues]] / [[git-find-old-version]] 互补:那边讲"AI 改了我的代码怎么找回",这边讲"任何来源的 git 问题怎么修"。
常见问题
- 二进制文件合并冲突无法手动 resolve 用 git checkout --theirs/--ours 选版本,不能用文本三方合并。
- Rebase 之后 commit 消失了 先看 git reflog;用 git cherry-pick 把丢的 sha 找回来。
- Force push 覆盖了队友的 commit GitHub 端用 events API 找到旧 sha;用 reflog + cherry-pick 还原。
- 我在 detached HEAD 上提交了,怎么办 git branch tempfix HEAD 立即保住 commit,再切回原分支。
- 历史里的大文件让 push 失败 用 git filter-repo 清掉,避免用过时的 BFG / filter-branch。
- Submodule 怎么都拉不到最新 commit git submodule update --remote --merge;检查 .gitmodules 分支名。
- Cherry-pick 解冲突后变成空 commit git cherry-pick --allow-empty 或 --skip;确认目标差异已存在。
- Stash 在 checkout 之后看不到了 git fsck --unreachable 找悬空 stash;git stash apply <sha> 恢复。
- 分支保护规则让我的 PR 合不了 Required check 名字变了;同步更新 protection rules。
- Tag 指向了错误的 commit git tag -f -a v1.2.3 <sha> + git push --force origin v1.2.3 修正。
- Monorepo partial clone 数据过期 git fetch --refetch;或 git sparse-checkout reapply。
- Git LFS pointer 文件没被真实文件替换 git lfs install + git lfs pull;检查 .gitattributes。
- git pull --rebase 让冲突历史变乱 用 rerere;遇到冲突用 merge commit 替代。
- 凭证 helper 锁住,pull / push 全失败 删除 ~/.git-credentials 锁;macOS Keychain 清条目。
- Clone 之后 git hooks 不执行 Hooks 不随 clone;用 husky 或 git config core.hooksPath。
- Revert merge commit 提示需要 -m parent git revert -m 1 <merge-sha>;选保留的主线。
- Bisect 在 skipped commit 上卡住 git bisect skip 当前;如全 skip 用 visualize 切换路径。
- 我把 secret 推到公开仓库了 立即 rotate;git filter-repo 清历史;联系 GitHub support。
- Worktree 在分支删除后变成幽灵 git worktree prune;用 --force 清理悬空目录。
- CRLF 转换让单次 commit 产生几千行 diff .gitattributes 显式 LF;用 --renormalize 一次性修。