Git In-depth Git In-depth

Detached Head & Dangling Commits

Check out a free preview of the full Git In-depth course:
The "Detached Head & Dangling Commits" 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 explains that a detached head occurs when a commit is checked out instead of a branch. If a commit does not point or associates to a branch, the changes will not be references in git and eventually go to garbage collection.

Get Unlimited Access Now

Transcript from the "Detached Head & Dangling Commits" Lesson

[00:00:00]
>> Nina Zakharenko: Now, this is a scary message that some of you might have seen. Because show of hands, who's been in a HEAD-LESS/DETACHED HEAD state. Yeah, it's scary, you're like my HEAD is DETACHED, am I dying? Is my gear repository going to blow up. What is going on here?

[00:00:18] So you'll see a DETACHED HEAD state when you check out a specific commit, or a tag instead of a branch. Git moves the HEAD pointer to the commit that you specified. When you check out a different branch or a commit, the value of HEAD's gonna change too, right?

[00:00:38] It's going to change to the next SHA. If you make any commits in a DETACHED HEAD state, they don't have any references to them. Git doesn't know how to point to them. Let me explain this with a diagram. I have a, I checked out a commit.
>> Nina Zakharenko: Git tells me that I'm in a DETACHED HEAD state.

[00:01:07] Can look around, make changes, I can commit them. If I don't do anything with the commits that I made in a DETACHED HEAD state, you can consider them lost. There are ways of getting them back and we'll talk about git disaster recovery in the afternoon, but if you don't do anything with them, poof.

[00:01:34] So, now I made a new commit, I added a second post.
>> Nina Zakharenko: And so I have a new commit, it's 1f6 and HEAD has moved to point at that commit.
>> Nina Zakharenko: What do we wanna do if we want to save our work? If we did work in a DETACHED HEAD, there's a few things we can do.

[00:02:06] We can create a new branch that points to the last commit we made in a DETACHED HEAD. Now that commit has a permanent reference of the new-branch-name. And, why just the last commit? Let's say you made multiple commits in a DETACHED HEAD state. Well, the very last commit knows about all of its parents, right.

[00:02:32] So we don't have to go through and try to save each of those commits individually. If we got the last one, we're in business.
>> Nina Zakharenko: Now you made some commits, you don't care about them. You can discard your work. If you don't point a new branch to those commits, they're not gonna be referenced in git anymore.

[00:02:55] Sometimes this is called a DANGLING COMMIT. Eventually they're gonna be garbage collected. This is pretty cool. Git does it's own garbage collecting. It runs every few weeks, if you want to you can run it manually. It'll go through and clean up any of those DANGLING COMMITS that you can't reference anymore.

[00:03:26]
>> Nina Zakharenko: So, if you lost those commits, there's still a way to get them back if garbage collection hasn't run yet. And that's with the ref log which we're gonna cover a little bit later.
>> Nina Zakharenko: So in this case, 1f6 is a dangling commit, and will be slated for garbage collection.