detached HEAD
The state where HEAD holds a commit ID directly instead of a branch name, so new commits are made but no branch moves to follow them.
Commits made while detached are easy to lose once you switch away.
In more detail
It happens when you switch to a commit, tag or remote-tracking branch, and it is a normal state for looking around, not an error. Commits made while detached are kept only as long as something points at them, so Git warns you when you leave. To keep them, create a branch with git switch -c before or after leaving.
Example
git switch --detach HEAD~1Related terms
- HEAD — Git's pointer to where you are: normally the name of the current branch, and through it the current commit.
- git switch — The command that moves you to another branch, updating HEAD and the working tree to match; -c creates the branch first.
- reflog — Git's local record of every position HEAD and each branch has pointed at, used to find and recover commits that no branch points at any more.
- branch — In Git, a movable name for one commit; committing on the branch moves the name forward to the new commit.
Taught in
- HEAD, And Why Detached HEAD Is Not An Error — Advanced Git Internals & Distributed Workflows
- Interactive Rebase: Squash, Reorder, Reword — Advanced Git Internals & Distributed Workflows