Technical Community

HEAD pointer in GIT: What You Need To Know

January 09 ,2020

In this post, we’ll acquire an understanding of the HEAD Pointer in Git. Git maintains a reference variable called HEAD. All capitals, H-E-A-D. We call this variable a pointer. What it does is to reference (or point to) a specific commit in the repository. As we make new more commits, the pointer changes (or moves) to point to the last new commit. 

The HEAD (pointer) always points to the tip of our current branch in the repository. The repository’s HEAD has nothing to do with the staging index or the working directory. It’s just a way of tracking the current position in the repository. A good example to use for the HEAD is the playback and record head on a cassette tape recorder. As we start to record audio, the tape moves past the head, and it records data onto it. When we hit the Stop button, the place where that record head is stopped is also the place that it will start recording again, if we hit the ÇRecord button a second time. 

Now we can move around and also move the head to different places, we can fast forward and rewind. When we hit record again, wherever the head is positioned is where it’s going to start recording. The HEAD pointer in Git is very similar. It points to the place where we’re going to start recording next. It’s the place where we left off in our repository with what we’ve committed. 

Let’s take a look at some illustrations that will make this clear. In the last post, we saw how we could refer to them using the SHA values.

Those 40-character unique values that are tied to these snapshots. Let’s break this down, just to make the illustration simpler and use the first six characters of each one of those SHAs. So here we can see the same SHAs linked together as a series of commits. 

Q: Can you guess where the HEAD points to after we’ve made these three commits? 

It points to the tip of the current branch in our repository. That’s the last commit that we made. It’s the parent of the next commit that we make. So we make another commit, it’ll be attached to the end there, after a614b5. 

That’ll become the parent. The HEAD becomes especially important when we start to look into branches. Branches is a more advanced subject we’ll not cover on this post, but let’s just touch some very basics of it here. 

By default, the branch we’re working on is called the master branch, and that’s our main branch. So let’s walk through the master branch and see how the HEAD pointer moves as we make our commits. 

Let’s start with our first commit 5c15e8. In the beginning, the HEAD pointer points to that 5c15e8. Then when we make a new commit, it knows that the parent is going to be 5c15e8. Git will run it all through the SHA hash algorithm to create a new commit with a new SHA, and it moves the HEAD pointer to point to that last commit (38e73d). That’s now the tip of our master branch. And then when we make another one (a614b5), it does the same thing and moves the head pointer again. It’s like it moved the record head of that tape recorder in our metaphor. 

In Git we have the ability to create new branches, which is to create a new set of code from what we’re working on, and it’s going to be separate from our master branch. We’ll learn how to do that in another post. But for now, let’s just say that we have a new branch and that new branch is going to start moving and have its own commits. 

When we make the first commit, the HEAD moves to that commit. And then when we make another commit, the HEAD moves to that one. And so on. Of course, we have the ability to move the HEAD back and forth to different branches, so that we can write on different branches, depending on where the head is located. 

Git will keep track of moving the HEAD around for us. It’s not something that we need to worry about. But to understand it deeply and see where it exists, let take a look at the .git (hidden) folder inside our project. 

Inside there, we’ll see that there’s a file called HEAD, in all capitals. It’s just a simple file, let’s take a look at what’s inside it. 

It says, ref : refs/heads/master. What this is doing is it’s telling it that it can find this information in this refs directory. So let’s take a look at what’s in that directory. Let’s type cat refs/heads/master and hit Enter.

And look at what it comes out with. It gives us a SHA value. 

Q: What is that SHA value? 

Let’s call git log, and let’s look at our first commit. Look at that, it exactly matches.

So it’s keeping track of the HEAD right here in this file, it tells us to look at this other file, refs/heads/master, in order to know what it points to. If we switched branches, then the HEAD would point to the tip of a different branch. 

We can see that Git tells us right here in the log, that the HEAD is pointing at master right now. Most of the time, we will not need to actually manipulate the HEAD ourselves. Git will take care of that for us. But it is useful to know about the concept and to understand what git is doing under the hook.

Conclusion

There is a pointer called HEAD which is basically a variable that Git uses to keep track of the tip of the current branch in the repository. When we make new commits, the pointer moves to the last new commit.

Also published on

Share post on