Git で detached HEAD 状態から戻れない・コミットを残せない
タグや SHA を直接 checkout すると detached HEAD になる。
何もしていなければ git switch <branch> で戻れるし、コミットしてしまった場合も git switch -c で新ブランチに退避すれば失わない。
公開: 更新:
git checkout v1.0.0 … You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.
git switch -c rescue/feature-x Switched to a new branch 'rescue/feature-x'
2026-09-29 に alpine/git(Git 2.54.0) で実際に打って取った出力。検証環境の詳細
要約
You are in 'detached HEAD' state. は、HEAD がどのブランチも指していないことを知らせる警告である。
コミットしていなければ git switch <branch> で離脱できる。
コミットしてしまった場合も、git reflog と git branch を組み合わせれば失わずに済む。
実行例
alpine/git のコンテナで git checkout v1.0.0 を実行すると、終了コード 0 のまま You are in 'detached HEAD' state. の案内が出た。git status も HEAD detached at v1.0.0 を返した。
detached のまま作ったコミット 5c7ae8d は、git switch -c rescue/feature-x で新しいブランチに残った。master に戻った後の git reflog にも commit: wip on detached の行として出ており、その SHA を渡した git branch rescue2 もエラーを出さずに終わった。
$ git checkout v1.0.0
Note: switching to 'v1.0.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 75b591d v1
$ echo $?
0$ git status
HEAD detached at v1.0.0
nothing to commit, working tree clean$ git switch -c rescue/feature-x
Switched to a new branch 'rescue/feature-x'
$ echo $?
0$ git log --oneline -n 3
5c7ae8d wip on detached
75b591d v1$ git reflog
c2093c9 HEAD@{0}: checkout: moving from rescue/feature-x to master
5c7ae8d HEAD@{1}: checkout: moving from 5c7ae8d9cb2d7ece381c3040500bcdae286b39ce to rescue/feature-x
5c7ae8d HEAD@{2}: commit: wip on detached
75b591d HEAD@{3}: checkout: moving from master to v1.0.0
c2093c9 HEAD@{4}: commit: v2$ git branch rescue2 5c7ae8d9cb2d7ece381c3040500bcdae286b39ce— 2026-09-29 時点の出力
検証環境Git 2.54.0 / 2026-09-29 検証
- 検証日
- 実行環境
alpine/gitAlpine Linux v3.24- バージョン
- Git 2.54.0
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. まだ何もしていない場合
git status
# HEAD detached at <sha>
git switch - # 直前ブランチに戻る
# または
git switch maingit switch - で直前にいたブランチに戻れる。
詳しい使い方は git-switch(新しいタブで開く) にまとめられている。
2. detached でコミットしてしまった場合
git switch -c rescue/feature-x # 今の HEAD を新ブランチに保存
git switch main # 安全に戻れるgit switch -c は 「新ブランチを作って切り替える」 ため、detached 中の作業をそのまま保護できる。
古い git checkout -b <name> と同じ働きをする。
3. すでに移動して見失った場合
git reflog
# c0ffee1 HEAD@{2}: commit: WIP
# ...
git branch rescue/feature-x c0ffee1reflog から SHA を拾い、ブランチを後付けする。
GC(既定 90 日)が走る前なら、基本的に救出できる。
4. submodule での detached
git -C path/to/sub switch main
git -C path/to/sub pullサブモジュールは登録 SHA に固定される設計になっている。
最新を追うときは、明示的にブランチに切り替える。
よくある原因
① ふつうの状態
② detached のままコミット
③ git switch -c で名前を付ける
- タグや SHA を直接 checkout した。
git checkout v1.0.0のようにブランチではない参照点を開くと、HEAD はどのブランチも指さなくなる。 - CI 環境が SHA で fetch している。
多くの CI は SHA 指定でgit checkout <sha>を打つため、常に detached になる。 - submodule を更新した。
git submodule updateの後、サブモジュールは登録 SHA を指す detached 状態になる。 - detached のままコミットした。
ブランチを切らずにコミットして別ブランチに移動すると、そのコミットは GC 対象になる。