Check out a free preview of the full Everything You'll Need to Know About Git course

The "HEAD & reflog" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how the "head" pointer changes when switching branches and shows the contents of the "head" file in the Git repository. The "reflog" command, which shows the history of branch changes, and how it is stored in the Git logs folder is also discussed. He also demonstrates how to view the reflog and provides an example of using the "reflog -3" command to display the last three entries.


Transcript from the "HEAD & reflog" Lesson

>> We're going to talk about HEAD, you've seen it already, it's been in the log, you see this constant head dash, whatever branch name. It probably is a little bit confusing, you should rarely use it if you don't have to, you should try not to play with it.

It always causes so many problems the moment you start checking things out but if you know how to use it, it's fantastic. I use it all the time for interactive rebasing I'll do like a head tilde six, I'm able to squash everything, it's fantastic, but it can also be a pain.

So throughout this course, you'll see this, so what is HEAD? We can do some experimenting, so what I want you to do is, I want you to check out trunk and then I want you to execute log one line. I'll add a little one line there, you'll see had points to trunk, everyone should see that, let's do it again.

I want you to checkout foo, and then I want you to do another git log one line, so get checkout foo, get log one line. Again head stamp pointing to foo, so it's like every time we change, branches, notice that head's pointing to whatever branch we have checked out.

Okay, this is important. Go check out the git folder and tell me what head is pointing to. So if you go in here and you go ls git, you'll see that that like named item is just a top level item in git. So if I cat git/HEAD, my goodness, just auto complete, you'll see right here it's a ref to foo, that's all it is.

It's just this thing that exists right there, it's just a pointer to whatever you're currently using, it's really nice. Once you see that it's just not that magical, that means if I go cat refs or git refs heads foo, you'll see it's just a commit. We're walking through, starting at HEAD all the way to the exact commit and that means I could reconstruct the repo now that I have this commit, and we did that earlier.

So we can actually go all the way from head to the exact file that we want to look at, and so this is kind of how that works. So it's good to have in your head how it works, didn't even know that was going to come out. All right, here we go Reflog, it's actually not a wizard tool, it's just not It feels magical, but it's very one-dimensional.

So Reflog just shows you where head's been, so give reflog a try, I just told you, I unfortunately just ruined the moment, just execute git.reflog. And just look at it, and see that it followed how you've changed your branch. So if I do it, if I go git reflog, you'll see that, look at that, moving from trunk to foo, from foo rebase to trunk, you can just watch me change each one of these.

And it comes with a commit shawl for each one of those items. This is when I was at B, remember when we do doof things up way earlier in the repo, you can actually see me doing, what's it called? A reset, you can actually watch me do all my oopsy daisies, we redid everything right here.

Look at that, so you can actually see the entire history of all decisions you've made in your repo by looking at reflog. And all it is is just simply the log of where you've moved, that's it, nothing magical about it, it's very simple. Do you think you can find where reflog is stored in the git folder, is it a log, how do they store it?

All right, so if I go here and I go like this, if I just ls git, you'll see, look at that there so logs folder, that feels pretty serendipitous, right? So what's in the old logs folder? Well look at that, there's one called HEAD, what do you think's in that file?

Let's just pretend that I don't know, and let's count it out, does that not look pretty dang familiar? Reflog is just prettifying this file, at least it is default basic usage, there's a lot of options, there's always a lot of options. In fact, you can even use git log to create reflog with a whole bunch of options but we're not gonna do that.

Just this right here is as simple as it gets at AOL, yeah, I did AOL for those that are laughing, I did, I thought it was funny, it's a great email, okay? All right, solution, you can do a reflog-3, and you can actually see three lines of your reflog.

So if you don't want the whole thing you just wanna see a little bit, you can actually give it how many lines you want. So if I go git reflog-3, I see three, that means if I cat out my head and go, tail-3, you can see the last three, and you'll notice, they kinda look, they're the same, right?

You can see here moving from trunk to foo moving from trunk to foo. So there you go, it's inverted, the last line of the file is the first line in reflog, it just takes the file and inverts it, that is it, so there you go. Now, hopefully again, the magic is being slowly spoiled and now the real magic happens.

Which is how simple git actually is and how powerful it really is that's where the real magic is in the use.

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