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

The "References Solution" 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 walks through solution to References Exercise.

Preview
Close

Transcript from the "References Solution" Lesson

[00:00:00]
>> Nina Zakharenko: Let's go ahead and review the solutions. So, here I am in the advanced git exercises branch. And I am going to check out exercise 3. The first thing I want to do is, figure out where my head variable is. Let's check it out by finding out the value of git slash head.

[00:00:27]
And we can verify this by running git branch. Now let's figure out what our references are pointing to. We can do that with the plumbing command, git show-ref. And we pass in the --heads [INAUDIBLE] Cool. So I have a few more exercises than you guys. If I filter this out to just exercise 3, we'll see that that is the Last commit in this branch.

[00:01:09]
>> Nina Zakharenko: And if we wanted to, we could use git cat file-p to verify this. So let's say I wanted to look at the reference for my master branch. It's this one right here. So I can copy this reference, and then do a git cat-file -p.
>> Nina Zakharenko: And that'll show us the last master commit.

[00:01:41]
Mine might be a little bit different than yours. Cuz I've been working on these exercises. But yours should be the initial commit.
>> Nina Zakharenko: Now, our exercise 3 branch is pointing to a newer commit from exercise2 We can verify that,
>> Nina Zakharenko: By taking a look at this commit using git cat-file -p.

[00:02:13]
>> Nina Zakharenko: And this is the commit from exercise two.
>> Nina Zakharenko: Now let's make a tag and we want to confirm that it points to the correct commit. We can name this tag whatever we want. Now, I can do get tag to show which tags I have. That should show my /git/commit and let's make sure that the tag is pointing to the right place.

[00:02:57]
If I use the plumbing command git show-ref, I can pass in the --tags flag instead of --heads like we did earlier. That'll show that my git tag is pointing to the current commit on the exercise3 branch.
>> Nina Zakharenko: We can also verify that, to look that up.
>> Nina Zakharenko: With git tag dash dash points-at.

[00:03:27]
And checking out this commit, cool. Those are all the tags that point at this commit right there. Now let's get a shot at trying out annotated tags. Remember that annotated tags serve the same function as a regular tag. But they contain some extra metadata, right? The author, the date, and an optional message.

[00:03:54]
So, to create an annotated tag we use the- a flag. And I'll say my-annotated-tag and I'm gonna pass in a message.
>> Nina Zakharenko: And I messed up my quotes. Let me try that one more time.
>> Nina Zakharenko: Great, now when I list my tags with git-tag, we'll see that I have a new one.

[00:04:31]
I can take to look at it with git show.
>> Nina Zakharenko: And that shows the commit that it's pointing to.
>> Nina Zakharenko: Now let's check out detached head. Now that you know what it really means, hopefully the terminology isn't so scary anymore.
>> Nina Zakharenko: Someone should go as a detached get detached head for a Halloween custom this Halloween.

[00:05:05]
October 31st is coming up. [LAUGH] I think you guys could scare some programmers. [LAUGH] So let's use get checkout to checkout to figure out the latest commit directly. And we can figure out what that is by looking at the log. And I use the dash dash, no pager here just so that prints out in my terminal instead of opening on the screen.

[00:05:41]
>> Nina Zakharenko: Cool. So this here is my latest commit.
>> Nina Zakharenko: I am going to check it out.
>> Nina Zakharenko: No detached head state. Look around make changes. I don't know who wrote this message but it's really not [LAUGH] helpful. Great so if we take a look at our head now we'll see that it is pointing at that commit.

[00:06:14]
Now if we create a dangling commit
>> Nina Zakharenko: And we add it.
>> Nina Zakharenko: So that's in our staging area. If we do a status, git will tell us that we're in a detached head state. And let's commit it.
>> Nina Zakharenko: Great, now if we, look at out log
>> Nina Zakharenko: We will see our latest commit, an example of a dangling commit.

[00:07:05]
>> Nina Zakharenko: And if we check our HEAD again,
>> Nina Zakharenko: Can anyone tell me what HEAD will be pointing at now? Which commit? The first one? The second one?
>> Nina Zakharenko: I pick you.
>> Speaker 2: E348EBC?
>> Nina Zakharenko: Not quite.
>> Speaker 2: I don't know that at all then. [LAUGH]
>> Nina Zakharenko: What's the latest commit we made?

[00:07:31]
>> Speaker 3: The dangling ten.
>> Nina Zakharenko: Yes, so now if we look at head, it's gonna be pointing at the last commit we made, the dangling git commit, great.
>> Nina Zakharenko: So if we wanted to keep this commit, we could check out a branch. But right now, we're just gonna throw it out.

[00:07:56]
So if we git checkout exercise 3, and I will show you the log one more time. So we made this commit while we were on a detached head. I I check out exercise three. It is going to give me a warning telling me that I am leaving one commit behind.

[00:08:20]
It's not connected to any of my branches. It doesn't have any references. And if I don't do anything about this eventually this commit is going to be garbage collected.
>> Nina Zakharenko: And it also gives you a helpful tip that if you want to keep it you can create a new branch.

[00:08:38]
You can say git branch branch name, and keep that commit.
>> Nina Zakharenko: Anyone have questions about references? Heads, detached heads, how are you feeling?
>> Nina Zakharenko: Seeing a lot of lost faces.

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