Sub topic 3
HEAD: where you are now
This chapter assumes you can run git init, git add, git commit, git status, and git log. You should also know that a branch is a named pointer to a commit. Git 2.23 or later is needed for git switch.
What HEAD is
HEAD is a special reference that tells Git where you are in the history. It answers two questions:
- Which commit is checked out in your working directory right now?
- Which commit becomes the parent of the next commit you make?
Most of the time, HEAD does not point at a commit directly. It points at a branch name, and the branch points at a commit. This is called a symbolic reference. You can see it in a file.
cat .git/HEAD
git symbolic-ref HEAD
git rev-parse HEAD
git rev-parse mainOn a repository where main is checked out, the first command prints ref: refs/heads/main. The second prints refs/heads/main. The last two print the same commit hash, because HEAD resolves through main to its tip commit.
How HEAD moves
HEAD moves in two ways.
- When you commit, Git creates a new commit whose parent is the current commit. Then it moves the branch that HEAD points to, so the branch advances. HEAD itself does not change. It still names the same branch.
- When you switch branches, Git changes what HEAD points to and updates your working directory and index to match the new branch's tip.
The following sequence shows both.
git switch main
git branch --show-current
git switch -c feature/login
git branch --show-current
git commit --allow-empty -m "Start login work"
git log --oneline --decorate -3The log output marks the tip with HEAD -> feature/login. The -> means HEAD is attached to that branch. The main label sits on an older commit, because only feature/login moved when you committed.
Reading the decorations in git log
git log --decorate labels commits with the references that point to them. The labels tell you a lot about the current state.
| Label | Meaning |
|---|---|
| `HEAD -> main` | HEAD is attached to the local branch `main`. |
| `origin/main` | The remote-tracking branch for `main` on `origin`. |
| `HEAD` alone, with no arrow | HEAD is detached and points directly at a commit. |
| `tag: v1.0` | A tag points at this commit. |
Use git log --oneline --decorate --graph --all when you want to see every branch at once. Without --all, the log shows only history reachable from HEAD.
Relative references from HEAD
You can name commits relative to HEAD. This is useful for inspecting recent history and for commands that take a commit argument.
| Expression | Meaning |
|---|---|
| `HEAD` | The current commit. |
| `HEAD~1` or `HEAD~` | The first parent of HEAD. |
| `HEAD~3` | Three commits back, following first parents. |
| `HEAD^` | The first parent of HEAD. |
| `HEAD^2` | The second parent. This exists only on merge commits. |
| `@` | A shorthand for `HEAD`. |
The difference between ~ and ^ matters only for merge commits. A merge commit has two parents. HEAD^1 is the branch you were on when you merged, and HEAD^2 is the branch you merged in. HEAD~2 means "go to the first parent, then its first parent." It does not mean "the second parent."
git show HEAD~1 --stat
git diff HEAD~2 HEAD
git log --oneline HEAD~5..HEADThe first command shows the previous commit and the files it changed. The second shows the combined change of the last two commits. The third lists the last five commits.
HEAD and the working directory
HEAD is one of three states Git compares. The others are the index (the staging area) and the working directory.
git diffcompares the working directory to the index.git diff --stagedcompares the index to HEAD.git diff HEADcompares the working directory to HEAD.
This is why HEAD matters when you switch branches. If you have uncommitted changes that do not conflict with the target branch, Git carries them along. If they would be overwritten, Git refuses and prints an error that lists the files. In that case, commit your work, or use git stash to set it aside, and then switch again.
Detached HEAD
HEAD is detached when it holds a commit hash instead of a branch name. You can check out a commit, a tag, or a remote-tracking branch, and Git will detach HEAD.
git switch --detach HEAD~2
git status
cat .git/HEAD
git symbolic-ref HEADgit status prints HEAD detached at <hash>. .git/HEAD now contains a raw commit hash. git symbolic-ref HEAD fails with fatal: ref HEAD is not a symbolic ref, which is a reliable way to test for this state in a script.
A later chapter covers detached HEAD in full. For now, remember the one real risk. If you commit while detached, the new commits are reachable only from HEAD. No branch points at them. When you switch away, they become unreachable, and Git will eventually remove them during garbage collection. Git warns you when you leave such commits behind.
To keep work done in a detached state, create a branch before you leave.
git switch -c rescue-branchTo leave a detached HEAD without keeping anything, switch to a branch.
git switch mainMoving the branch that HEAD points to
Because HEAD names a branch, commands that move "the current branch" are really moving the branch that HEAD is attached to. git reset is the main example.
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1Each form moves the current branch back one commit. They differ in what happens to the index and working directory:
--softleaves both alone, so the undone changes stay staged.--mixed, the default, resets the index but keeps your files, so the changes become unstaged.--hardresets both, so the changes in tracked files are lost.
--hard is the dangerous one. The commits you moved away from are still recoverable through the reflog for a while, which is covered in the last chapter.
Common errors
- Assuming HEAD is the branch. HEAD usually names a branch, but it is a separate reference. Check with
git symbolic-ref HEADwhen behavior surprises you. - Confusing
HEAD~2withHEAD^2. Use~for "n generations back" and^for "which parent of a merge." - Committing while detached and then switching away. Create a branch with
git switch -c <name>first. - Reading
git logwithout--alland thinking commits are missing. The default log shows only ancestors of HEAD. - Using
git reset --hardwith uncommitted changes. Those changes are not in any commit, so the reflog cannot restore them.
Where to find the history of HEAD
Git records every movement of HEAD in the reflog. Each switch, commit, merge, and reset adds an entry.
git reflog
git reflog show HEAD@{2}HEAD@{2} means "where HEAD was two moves ago." This is different from HEAD~2, which follows commit ancestry. The reflog is local to your repository and is not shared when you push.
Exercise
You are on main at commit C5. You run git switch --detach HEAD~2 and then git commit --allow-empty -m "Experiment". You then run git switch main. Which reference points to the "Experiment" commit, and what must you do to keep it?
Show answer
No branch points to it. Only the detached HEAD pointed to it, and switching to main moved HEAD away, so the commit is now unreachable. Before leaving, run git switch -c experiment to attach a branch to it. After leaving, find its hash with git reflog and run git branch experiment <hash>.
Exercise
A merge commit M has parents A (the branch you were on) and B (the branch you merged in). Which expression names A, and which names B?
Show answer
M^1 (or M^) names A, and M^2 names B.