
On the other hand, when a branch points directly to a specific commit, it puts you in a git detached HEAD state. Hence, when on a branch, the HEAD always points to the latest commit made on that checked-out branch. The HEAD points to the active branch reference - the feature_branch in this case - where the latest commit is made. There is also a feature branch we switched or checked out into from the master branch, as seen above. The local repository usually starts out with a master branch. The image above shows we have a master branch and a feature branch. Let’s visualize what a HEAD on a git branch looks like below: It points to the current or active branch reference. When you switch or checkout to a branch, the HEAD points to the latest commit made on that branch. It can point to either the latest commit on a branch or to a specific commit directly.
#Git create new branch and move changes code#
For example, you can use it to test out some experimental code logic or even bisect a commit that you think might have introduced some bugs into the project’s codebase.Ī git HEAD is simply a pointer that points to a specific version or state of a git repository. A git detached HEAD has a few useful use cases.You can generally get out of a git detached HEAD state by simply running the git checkout or git switch command.There are also several ways to get to this state, but the common scenario is checking out to a commit via its hash. A git detached HEAD state occurs when you are not on a branch but directly on a specific commit.To understand what a git detached HEAD is, let’s first take a look at what a HEAD is in git.


Have you ever paused for a few seconds, staring at your screen with bewilderment, trying to figure out the informational message Git wrote to the console? And how you got to the state Git says you are in? You are not alone! We have been there, and that’s exactly what a git detached HEAD does to you, especially when you encounter it for the first time.
