You’ll encounter the term “HEAD” as you use Git, no matter what commands you run. You’ll find this term widely used throughout the Git documentation. What does it mean?
In this guide, we’re going to answer that question. We’ll discuss, in detail, what a Git HEAD is and how you can find out what HEAD you are viewing. We’ll walk through a few commands that will help you deepen your understanding of Git HEADs.
What is Branching?
Before we begin, we must talk about branching. Git lets you divide your codebase over Git branches. Each branch is an independent line of development on which you can code.
Having branches lets you work on different versions of a repository at the same time. One branch can host the “main” code for your repository, which is the version you’d use in production. Another branch may contain the code for a bug fix, or the code for a new feature.
Separate lines of development help with version control on a new project.
It would not be wise to make a bug fix directly to the main version of a codebase before that bug fix has been tested and reviewed by all the necessary parties. By having a branch on which the bug is fixed, a discussion can go on about how to fix the bug that is away from the main code.
What is a Git HEAD?
The term HEAD refers to the current commit you are viewing.
By default, you’ll view the tip of the master branch on a repository, unless the main branch of your repository has a different name. The tip of the master branch is the most recent commit on the main branch of your codebase.
You can find out what HEAD you are viewing by opening the .git/HEAD file in your repository:
The cat command shows us the contents of our HEAD configuration file:
The final word in the file, “master”, tells us the branch we are viewing.
You can switch branches and commits by using the git checkout command to change your HEAD pointer in Git:
git checkout dev
This will move us onto the “dev” branch in our repository. If we view the .git/HEAD file again, we’ll see that its contents have changed:
This file now says that we are viewing the “dev” branch in our working directory.
Git HEADs and Detached HEADs
Git HEADs can represent a particular commit in the history of a project. This is because Git lets you check out different points in a repository’s history to view how your project has evolved.
We can check out a specific commit using the checkout command. This time, we’re going to specify the hash pointing to the commit rather than the name of a branch:
git checkout 9ad202d538c6ee6448e2c1ecc080c292cc761771
This command will check out a commit from our repository’s history. When we run this command, we enter into what is called a detached HEAD state. This means we are viewing a commit rather than a branch.
In detached HEAD state, we can look around our repository. We can make changes to our files as they appeared in the commit we are viewing. We can save the changes we make by creating a new commit.
Let’s open up our .git/HEAD file again and see what it says:
Instead of pointing to a branch ref (i.e. “refs/heads/dev”), our HEAD now points to a particular commit. We can move back to our main branch by running git checkout again:
git checkout master
This moves us back to the “master” branch.
HEAD vs. head
The HEAD is the commit or branch you are presently viewing. Notice we have used all capital letters to denote this status.
You may see “head” written in lowercase. When “head” is written in lowercase, it can refer to any one of the “heads” in a repository. For instance, “master” is a “head” because it is a reference to a branch.
If we are viewing the master branch, then “master” is also our HEAD. If we are not viewing the master branch, then whatever branch or commit we are viewing is our HEAD.
A repository can contain a number of heads but only one HEAD.
This may sound confusing. Let’s summarize HEAD vs. head in a sentence: A HEAD in all caps is a reference or commit in your repository that you are viewing, whereas a “head” with no caps is a head that you are not viewing.
Git relies on the HEAD concept to track what commit or reference you are viewing. You can change your head by checking out another branch or a commit in your repository.
If you check out an old commit, you’ll enter a detached HEAD state. This is when you are no longer viewing the tip of the current branch (the most recent version of a branch). Instead, you are viewing how a repository appeared at a particular point in time.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more