Check out a free preview of the full Git In-depth course

The "References" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

Nina reviews references in git: tags, branches, and HEAD.

Preview
Close

Transcript from the "References" Lesson

[00:00:00]
>> Nina Zakharenko: References are pointers to commits. We already learned that there are three types of Git references. Tags, annotated tags, branches, and HEAD. So now let's talk about, let's get a little bit philosophical. What is a branch? So a branch, it's just a pointer to a particular commit. The pointer to the current branch is going to change.

[00:00:32]
I'm sorry, the pointer of the current branch is going to change as new commits are made. What is HEAD? HEAD is how Git knows what branch you're currently on. But most importantly, HEAD is how Git knows what the parent of the next commit will be. It's kind of effectively a sim link to the branch that you're currently working on.

[00:01:05]
>> Nina Zakharenko: It's a pointer. It points to the name of the current branch. But there's one more thing that can happen. It can point to a commit too and that will get us into a detached HEAD state.
>> Nina Zakharenko: It also moves when you either make a commit to the currently active branch, or when you check out a new branch.

[00:01:38]
>> Nina Zakharenko: Now, in the first exercise, when I check out my master branch, we looked at the value of .git/HEAD under the hood. So, when I'm on the master branch, the HEAD will point to refs/head/master. When I check out a feature branch, when I look at the value of HEAD again, now it's pointing to refs/heads/feature.

[00:02:14]
>> Nina Zakharenko: So for the slides, I have a little sample repo, just a simple blog. Index.txt contains links to the entries as well as our post titles. And the post directory contains our blob entries. Simple, simple. Nothing complicated here. I made the directory posts and made a welcome.txt post, and I have an index.txt that says that the title of that post is welcome to my blog.

[00:02:49]
So the current state of my repo, if I get in it and I add that those two files in that one directory, I have an initial commit. In this case, cd0b.
>> Nina Zakharenko: Now when I check out a new branch, I was on the master branch. When I check out that new branch, it's gonna point to master.

[00:03:18]
It's going to point to the same commit that master was pointing at. And HEAD is now going to point at tech posts. So my current repo state. Let's say I add a new post about Python and I make a commit. That commit 2b0 has a parent commit, cd0b.

[00:03:53]
Now tech_posts branch has moved, right? Master is still on that initial commit branch. And because we're on the tech_post branch, HEAD has moved along it.
>> Nina Zakharenko: Does that make sense? Does anyone have questions about this? You guys all look like you're deep in thought which is great. Cuz this is really important concept to understand when we are working with Git, but it's also pretty simple.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now