What Is the Head in Git?

Steven Hough
2 min readDec 17, 2020

--

HEAD is a ref (reference) to the currently checked out commit.

layman’s terms of HEAD:-

  • HEAD means (the reference to the) current commit
  • HEAD~1 means (the reference to) 1 commit before
  • HEAD~ ALSO means (the reference to) 1 commit before
  • HEAD~87 means (the reference to) 87 commits before
  • HEAD~3..HEAD means from 3 commits to current commit

In normal states, it’s actually a symbolic ref to the branch you have checked out — if you look at the contents of .git/HEAD you’ll see something like “ref: refs/heads/master”. The branch itself is a reference to the commit at the tip of the branch. Therefore, in the normal state, HEAD effectively refers to the commit at the tip of the current branch.

It’s also possible to have a “detached HEAD”. This happens when you check out something besides a (local) branch, like a remote branch, a specific commit, or a tag. The most common place to see this is during an interactive rebase, when you choose to edit a commit. In detached HEAD state, your HEAD is a direct reference to a commit — the contents of .git/HEAD will be a SHA1 hash.

Generally speaking, HEAD is just a convenient name to mean “what you have checked out” and you don’t really have to worry much about it. Just be aware of what you have checked out, and remember that you probably don’t want to commit if you’re not on a branch (detached HEAD state) unless you know what you’re doing (e.g. are in an interactive rebase).

Usage:

  • git checkout HEAD~1 will actually GO/checkout to 1 commit/reference before
  • git reset HEAD~3 will uncommit your last 3 commits — without removing the changes, ie you can see all the changes made in the last 3 commits together, remove anything you don't like or add onto it and then commit them all again.
  • git reset --hard HEAD~3 will uncommit your last commit and remove their changes. It will completely remove those changes. For more see here.
  • git diff HEAD~3 for checking changes in the last 3 commits
  • git revert --no-commit HEAD~3..HEAD. Make a new commit by reverting last 3 commits

--

--

Steven Hough

Software Engineer and Blogger: Code Creator and Word Weaver