HEAD
- Pronunciation
- HED
In short
HEAD is Git's pointer to what you have checked out, normally the current branch, which points to its latest commit; new commits are added where HEAD points.
What is HEAD in Git?
Git stores HEAD in the file .git/HEAD. Most of the time it contains a reference to a branch, such as ref: refs/heads/main, so HEAD means "the tip of the branch I'm on". When you commit, the branch moves forward to the new commit and HEAD moves with it, because it follows the branch.
HEAD is also the starting point for relative references. HEAD~1 is the parent of the current commit, HEAD~3 three commits back, and HEAD^2 the second parent of a merge commit. Commands use it everywhere: git diff HEAD shows all changes since the last commit, git reset --soft HEAD~1 undoes the last commit but keeps its changes staged, and git show HEAD displays the latest commit.
If you check out a commit, tag or remote branch directly instead of a local branch, HEAD points straight at a commit. That is a detached HEAD: you can look around and even commit, but those commits belong to no branch and are easy to lose, so create a branch with git switch -c if you want to keep them.
A common misconception is that HEAD always means the latest commit in the repository. It is only where you are right now. Git also keeps related markers, such as ORIG_HEAD, set before risky operations like reset and rebase so you can go back, and FETCH_HEAD, which records what the last fetch brought in.
Key takeaways
- HEAD points to what is checked out, normally the current branch.
- New commits are added where HEAD points, moving the branch forward.
- HEAD~1, HEAD~3 and HEAD^2 refer to commits relative to it.
- Pointing HEAD directly at a commit gives a detached HEAD.
- ORIG_HEAD and FETCH_HEAD are related markers Git sets automatically.
Example
cat .git/HEAD
# ref: refs/heads/main ← HEAD follows the main branch
git log --oneline -3
# 9f2c1ab (HEAD -> main) Add search
# 41d0e77 Fix login redirect
# c83b5a2 Initial commit
git show HEAD~1 --stat # the commit before the latest
git diff HEAD # everything changed since the last commit
git reset --soft HEAD~1 # undo the last commit, keep its changes stagedReaders ask
What does HEAD~1 mean?
The parent of the commit HEAD points to, one step back in history. HEAD~2 is two steps back, and so on. For merge commits with two parents, HEAD^2 selects the second parent.
What is a detached HEAD?
A state where HEAD points directly to a commit instead of to a branch, for example after checking out a tag or an old commit. Commits made there are not on any branch until you create one.
What is the difference between HEAD and main?
main is a branch: a pointer to a commit. HEAD points to the branch you are currently on. When you are on main they refer to the same commit, but after switching branches HEAD points to the new branch.
See also
- Detached HEADVersion Control, p. 7A detached HEAD is a Git state in which you have checked out a specific commit instead of a branch, so any new commits you make don't belong to any branch.
- BranchVersion Control, p. 2A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- Git ResetVersion Control, p. 20Git reset is a Git command that moves the current branch back to an earlier commit and, depending on its mode, also resets the staging area and your files.
- GitVersion Control, p. 10Git is a free, open-source distributed version control system that tracks changes to files over time, so developers can collaborate and undo mistakes.
- Git CheckoutVersion Control, p. 12git checkout switches to another branch or commit, or restores files to an earlier version; newer Git splits these jobs into git switch and git restore.
Spotted a mistake or something missing on this page?Suggest an edit