Junior — Middle
Explain what the Detached Head state in Git means and when it occurs.
sobes.tech AI
Answer from AI
The Detached Head state in Git occurs when the HEAD pointer does not point to a branch, but directly to a specific commit. This happens, for example, if you switch to a commit by its hash or to a tag, rather than a branch. In this state, any commits will not be associated with a branch and may be lost when switching branches.
Example:
git checkout <commit-hash>
Now you are in Detached HEAD. To save changes, you need to create a new branch:
git checkout -b new-branch