

When you add a new commit, your branch reference is updated to point to it, but HEAD remains the same. Most of the time, HEAD points to a branch name. So, what does it mean for it to be attached or detached? You’ve just seen that HEAD in Git is only the name of a reference that indicates the current point in a repository. When you create a new commit, its parent is indicated by where HEAD currently points to. In other words, HEAD answers the question, “Where am I right now?”įor instance, when you use the log command, how does Git know which commit it should start displaying results from? HEAD provides the answer. The purpose of HEAD is to keep track of the current point in a Git repo. HEAD is another important type of reference. The main objects in a Git repository are commits, but other objects include blobs and trees. The most important references in Git are branches, which you can think of as labels you put on a commit. Objects have relationships with each other, and references point to objects and to other references. What does HEAD mean in Git? To understand that, we have to take a step back and talk fundamentals.Ī Git repository is a collection of objects and references.

Before we go on, make sure you keep this in mind: you get to the detached HEAD state by checking out a commit directly. Use the log command to find it.Īfter running the command, we get the “You are in ‘detached HEAD’ state” message. Keep in mind that if you’re following along by executing these commands on your own system, the hash for your commits will be different from those in the example. So, based on the example output above, we’d run git checkout 87ec91d. Remember, branches are just names for commits. How would we do that? As it turns out, we can check out a commit the same way we’d check out branches. Let’s say that, for testing purposes, we need to see how things were at the time of the second commit. If you use the git log –oneline command, you’ll see something like this: Then we created a new empty file and committed that with the message “Create file.” Next, we added a line to that file and committed the change, with the message “Add a line to the file.” Finally, we’ve created another file with one line of text and committed that as well. With the commands above, we’ve created a new folder with a new repository inside it. git commit -m "Create file" echo "Hello World!" > file.txt git commit -a -m "Add line to the file" echo "Second file" > file2.txt git add.

Mkdir git-head-demo cd git-head-demo git init touch file.txt git add. We’ll create a repository and add some commits to it:
#GIT MEANING HOW TO#
Let’s start with a quick demo showing how to reach the detached HEAD state. Git Detached HEAD: Reproducing the “Problem” By the end of it, you’ll have a better understanding of Git’s fundamentals, and detached HEADs will never trouble you again. What does HEAD mean in Git? What does it mean for it to be attached or detached? These are the kind of questions we’ll answer in this post. If you just want to do that and get on with your day, go to the “How Do I Fix a Detached Head in Git?” section of this post to see how it’s done.īut if you want to know more-and I guess you do-stick around and we’ll help you. The second thing you need to know is that going back to normal is super easy. Sure, it’s not the normal state, which would be-you’ve guessed it!-when HEAD is attached.

The expression “Detached HEAD” might sound somewhat bizarre, but it’s a perfectly valid repository state in Git. Your repo isn’t broken or anything like that. Here’s the first thing you should know: you haven’t done anything wrong. If that’s your case, you’ve come to the right place. After coming across this message, most people start furiously Googling “git detached HEAD,” “git detached HEAD fix,” or similar terms, looking for anything that might be of help. The “You are in ‘detached HEAD’ state” one is certainly one of the weirdest. Newcomers to Git often get confused with some of the messages that the VCS tool throws at them. The following is a guest blog post written by Carlos Schults. "Git detached head" is a weird error message to receive.
