detached HEAD | ブランチから外れる (★★)

難易度 :star: :star:

:bulb: "detached HEAD"はgitコマンドではありません、練習問題にてdetached HEADを再現するための手順を学習しましょう

概要

detached head 状態のイメージ
  • ブランチから外れた場合に "You are in 'detached HEAD' state." というメッセージが表示される

  • 新規にブランチを作成するか適当なブランチをcheckoutすることでdetached HEAD状態を回避 = attached HEAD状態に戻ること出来る

  • gitにおけるbranchとは「commitを指すポインタ」

  • HEADとは現在自分が見ているcommitまたはbranchを指す「ポインタ」

  • つまり "detached HEAD" 状態とは「HEADがbranchを指してない状態 = 単独のコミットを指している」状態を指す

例題 (リポジトリ: git-drill/detached-head)

  1. :mag: このリポジトリではある日の業務日報が記された report.txt が管理されています。

  2. :mag: また3つのブランチが存在します。

    • master (= checkout直後のブランチ)

    • good-day (良いことがあった日に書かれた日報)

    • bad-day (悪いことがあった日に書かれた日報)

  3. :octocat: 各ブランチをcheckoutしたときに detached HEAD 状態にならないことを確めてください。

  4. :octocat: good-day ブランチをcheckoutした後に以下のコマンドを順に実行することで、 detached HEAD 状態になった後 detached HEAD 状態ではなくなることを確かめてください。またその時の状態をページ先頭にあるような図で説明してみましょう。

    1. git checkout HEAD~1

    2. git status

    3. git checkout good-day

  5. :octocat: bad-day ブランチをcheckoutした後に以下のコマンドを順に実行することで、 detached HEAD 状態になった後 detached HEAD 状態ではなくなることを確かめてください。またその時の状態をページ先頭にあるような図で説明してみましょう。

    1. git checkout HEAD~2

    2. git status

    3. git checkout -b soso-day

例題回答(No.3)

  1. git checkout good-day を実行し以下の表示が出ることを確認。

  2. 続けて git status を実行し、 detached HEADのメッセージが表示されないことを確認。

  3. 上記 1. と 2. の手順を bad-daymaster に対しても実行し表示を確認

例題解説(No.3)

この例題で行った処理はすべて既存のブランチをcheckout作業する作業です。 したがってHEAD(自分が今見ている対象)はブランチのみのためdetached HEAD状態にはなりません。

例題回答(No.4)

  1. git checkout good-day

  2. git checkout HEAD~1"You are in 'detached HEAD' state."の表示が出ることを確認。

    ```

    Note: checking out 'HEAD~1'.

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 performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b

HEAD is now at 72ce929 良い日の昼の出来事を追記

HEAD detached at 72ce929 nothing to commit, working tree clean

Previous HEAD position was 72ce929 良い日の昼の出来事を追記 Switched to branch 'good-day'

Note: checking out 'HEAD~2'.

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 performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b

HEAD is now at 662c712 悪い日の朝の出来事を追記

HEAD detached at 662c712 nothing to commit, working tree cleau

Switched to a new branch 'soso-day'

```

状態のイメージ図は下の解説にて

例題解説(No.5)

参考リンク

Last updated