detached HEAD 发生在 HEAD 直接指向一个 commit 而不是一个分支时。这是一个正常状态(不是错误),当您检出特定的 commit、tag 或 remote commit 时发生 — 但在其中提交存在丢失工作的风险,所以理解它很重要。
HEAD 通常指向什么
NORMALLY: HEAD → a BRANCH (e.g. main) → a commit
→ when you commit, the branch pointer advances, and HEAD follows it
DETACHED: HEAD → directly to a COMMIT (not through a branch)
→ you're "not on any branch" — looking at a specific commit
何时出现 detached HEAD
git checkout <commit-hash>
git checkout v1.0.0
git checkout origin/main
