Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we'll gain an understanding of the HEAD pointer in Git. Git maintains a reference variable called HEAD, all capitals, H-E-A-D. And we call this variable a pointer, because its purpose is to reference, or point to, a specific commit in the repository as we make new commits the pointer is going to change or move to point to a new commit. HEAD always points to the tip of the current branch in our repository. Now this has to do with our repository, not our staging index, or our working directory, we're talking just about the repository the commits that we've actually made to the repository by checking them in.
Another way to think of it is the last state of our repository or what was last checked out, and because it's where the repository left off or the last state, you can also say that the HEAD points to the parent of the next commit or it's where commit writing is going to take place. I think a good metaphor to think about this is the playback and record head on a cassette tape recorder. As we start recording audio the tape moves past the head, and it records onto it, when we press Stop the place where that record head is stopped is the place it'll start recording again when we press Record a second time.
Now we can move around, we can move the head to different places, but wherever the head is positioned when we hit Record again that's where it's going to start recording. The HEAD pointer in Git is very similar, it points at the place where we're going to start recording next. It's the place where we left off in our repository for the things that we've committed. Let's take a look of some illustrations that will make this clearer. In the last movie we saw how we could refer commits by using the SHA values, and I gave you an illustration like this. Now I want to distill this down and make these three different commits just the first six characters of each of the shots, so I'm going to represent each one of these three by just having 5c15e8, 38e73d, and a614b5.
Now can you guess where the HEAD points 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. So when we make another commit will be attached to the end there a614b5 will be the parent, and then we'll make the new commit. Now HEAD becomes especially important when we start talking about branches and branches is something that we'll talk about little later on, but I want to go ahead and just give you the basics of it here. By default the branch that we're working on is the master branch that's our main branch, so let's walk through the master branch and see how the HEAD pointer moves as a make our commits.
So we're going to start out with 5c15e8 as the first commit. At that point the HEAD pointer points to that commit. When we make a new commit it says, oh the parent is going to be 5c15e8, it puts in that new commit, and it moves the HEAD pointer to point to that last commit, the tip of our master branch. And then we make another one it does the same thing it attaches it to the end and moves the HEAD pointer. It's essentially like it moved the record head of the tape recorder forward. Now in Git we have the ability to create new branches that is to create a new set of code that we're working, and it's separate from our master branch, we'll learn how to do that later, but for now let just say that we have a new branch, and that new branch is going to start having its own commits that are separate from master.
When we make the first commit HEAD moves to that commit, and then we make another commit HEAD moves along that and along that. Now we can switch between these branches we can check out one or the other, so if we've have our new branch checked out well then HEAD is pointing at the last commit of the current branch, new branch. If we check out our master branch and switch back there. Well guess what? HEAD moves to point to the tip of the current branch that we have checked out their, master. And if we were to check out new branch again it would switch back and forth.
So HEAD always points to the tip of the currently checked out branch from the repository, don't worry too much of this branching stuff doesn't make total sense just yet we'll get to that later on, and you can always come back and review this movie than if you need to. Let's go to the command line and see how Git actually keeps track of HEAD. So here I am inside my first Git project directory, and you'll remember that inside there we had a folder called .git. So let's change into that folder and just take a peek here, you don't have to follow along with me, if you don't want you can just watch.
Let's look what's in that directory and look here there is a file called HEAD, and that's what Git uses to know where the HEAD is pointing. What is HEAD currently pointing to? I'll take a look at what's inside that file just say cat HEAD, and you'll notice that it says it refers to refs/heads/master, it doesn't point to a current commit, it points to a current branch, but that current branch has a tip it has a latest one, and that is maintained in this refs folder. So it's pointing into this folder, let's go inside refs, let's take a look at what's in there, you'll see that it has heads, and it tells us it's inside refs/heads and master.
So sure enough if we go in heads, we'll see that there is something in there called master, and if we take a look at master, there it is there is the commit that's the current commit that HEAD is pointing to. so it says all right, Git you want to know where HEAD points? Go look in a file called HEAD. HEAD tells me to go look inside refs/heads/master, and when I look in that file refs/heads/master points to 7aa195bd44ea et cetera, yours maybe different. Now what is that commit? Well, if we take a look at our log, git log, there it is 7aa195bd44a.
So that's the first commits the initial commit that I've made that's where it points to. You can use HEAD a lot in Git as a reference to the tip of the current branch, right. That's what we're saying when we say HEAD is the tip of the currently checked out branch. And in fact when I say git log it's actually the same thing is saying git log HEAD. Starting at the HEAD tell me what's their, start logging from the HEAD going backwards. So I think it's important to demystify this concept of HEAD. I think a lot of people start with Git and don't have a good understanding of what the HEAD is at the beginning. And then when it starts popping up later on, you start seeing references to HEAD you're like, wait a minute what is this thing, what is HEAD? Well, now you know.
Now you have an understanding that HEAD is just a pointer that goes to the tip of the current branch in the repository.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98327 Viewers
61 Video lessons · 85625 Viewers
71 Video lessons · 69566 Viewers
56 Video lessons · 101898 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.