Understand HEAD in Git

Distinguish an attached HEAD from a detached checkout, interpret parent and reflog notation, and keep experimental commits reachable.

Best practices
Illustration of Git branches and version history

HEAD identifies your current position in a Git repository. Usually it is a symbolic reference to the current branch, which in turn names a commit. In a detached checkout, HEAD refers directly to a commit instead.

When HEAD points to a branch

git symbolic-ref HEAD
# refs/heads/main
git rev-parse HEAD

A new commit normally advances that branch and therefore the commit resolved through HEAD. In a newly initialized repository, HEAD can name an unborn branch before any commit exists; resolving HEAD as a commit then fails.

Use Git commands for inspection rather than assuming .git/HEAD is a regular file at that path. Linked worktrees and submodules can use a .git file that points to the actual metadata directory.

When HEAD is detached

git switch --detach a1b2c3d
git status

Replace the ID with a real commit. Checking out a tag also commonly produces a detached checkout. You can inspect, build, and commit here, but new commits do not automatically advance a named branch.

To keep the current detached work on a branch:

git switch -c experiment

If you switch away without creating a ref, commits may still be recoverable through a local reflog for a time. Unreachable objects can eventually be pruned, so create a branch or tag for work you intend to retain.

Read relative revision notation

Expression Meaning
HEAD The current commit, if one exists
HEAD^ or HEAD~1 The first parent
HEAD^2 The second parent of a merge
HEAD~3 Follow the first parent three times
HEAD@{2} The value two entries back in HEAD's local reflog
@{u} The current branch's configured upstream, if available

Parent navigation follows commit ancestry; reflog navigation follows local ref history. A checkout or reset can create a reflog entry without creating a commit. Quote expressions such as 'HEAD@{2}' when needed to keep your shell from interpreting them.

Inspect the checkout state

git status describes a detached checkout, and git branch marks the current state. git symbolic-ref -q HEAD succeeds for a symbolic branch reference and returns nonzero when HEAD is detached.

git reset --hard HEAD leaves the resolved commit in place while resetting the index and tracked working-tree content to it. It discards uncommitted tracked changes and can remove untracked paths that obstruct the reset; it does not delete every untracked file. Save work before using it as a cleanup command.

See Git revision notation, git-symbolic-ref, git-switch, and git-reset.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting