
Here's a preview from my zine, How Git Works! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

read the transcript!
how git knows what your current branch is: .git/HEAD
.git/HEAD
is a file where git stores either:
- a branch name: the current branch
- a commit ID: this means you don’t have a current branch. git calls this “detached HEAD state”
by itself, .git/HEAD being a commit ID is okay
Illustration of a smiling stick figure with short curly hair.
person: it’s a great way to look at an old version of your code!
I don’t do it often, but it’s super useful!
git does it internally during a rebase!
the only problem is that new commits you make can get “lost” (page 13)
Illustration of five dots in a vertical stack, connected by lines. The top dot is labelled “main” and the bottom dot is labelled “HEAD”. There is a dotted line branching off from “HEAD”. The dot at the end of the dotted line is labelled “new commit will go here. danger! it won’t be on any branch!”
ways you can end up in detached HEAD state
You will end up in detached HEAD state if you checkout:
- a tag
$ git checkout v1.3
- a remote-tracking branch
$ git checkout origin/main
- a commit ID
$ git checkout a3ffab9
if you accidentally create commits in detached HEAD state, it’s SUPER easy to avoid losing them
just create a new branch!
git checkout -b oops
(you can also create a branch with git switch -c
if you prefer)
git has a little language for referring to commits
- the current commit:
HEAD
- the previous commit:
HEAD^
- 3 commits ago:
HEAD^^^
- 3 commits ago:
HEAD~3
The full documentation is at:
man gitrevisions
Saturday Morning Comics!
Want another comic like this in your email every Saturday? Sign up here!