[Git] “detached HEAD”となり、SHA1を参照してしまう

checkout時、コミットSHA1を指定、若しくは誤ったブランチパスを指定すると、”detached HEAD”の状態となることがある。

$ git checkout origin/feature/ph1.5-cms-format-change
Note: checking out 'origin/feature/ph1.5-cms-format-change'.

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 <new-branch-name>

HEAD is now at 8xxxxx... xxxを対応しました。

$ git status
HEAD detached at origin/feature/ph1.5-cms-format-change
nothing to commit, working tree clean

この時、対象ブランチではなく、ブランチのHEAD SHA1を参照している。

$ git name-rev 8xxxxx
8xxxxx remotes/origin/feature/ph1.5-cms-format-change


$ git checkout feature/ph1.5-cms-format-change
Branch feature/ph1.5-cms-format-change set up to track remote branch feature/ph1.5-cms-format-change from origin.
Switched to a new branch 'feature/ph1.5-cms-format-change'

$ git status
On branch feature/ph1.5-cms-format-change
Your branch is up-to-date with 'origin/feature/ph1.5-cms-format-change'.
nothing to commit, working tree clean