Git Head

The HEAD points out the last commit in the current checkout branch. It is like a pointer to any reference. The HEAD can be understood as the “current branch.” When you switch branches with ‘checkout,’ the HEAD is transferred to the new branch.

Git Head

The above fig shows the HEAD referencing commit-1 because of a ‘checkout’ was done at commit-1. When you make a new commit, it shifts to the newer commit. The git head command is used to view the status of Head with different arguments. It stores the status of Head in .git\refs\heads directory. Let’s see the below example:

1. Git Show Head

The git show head is used to check the status of the Head. This command will show the location of the Head.

Syntax:

$ git show HEAD
[/code]

Output:

Git Head

In the above output, you can see that the commit id for the Head is given. It means the Head is on the given commit.

Now, check the commit history of the project. You can use the git log command to check the commit history. See the below output:

Git Head

As we can see in the above output, the commit id for most recent commit and Head is the same. So, it is clear that the last commit has the Head.

We can also check the status of the Head by the commit id. Copy the commit id from the above output and paste it with the git show command. Its result is same as git show head command if the commit id is last commit’s id. See the below output:

Git Head

The above output is the same as git show output.The HEAD is capable of referring to a specific revision that is not associated with a branch name. This situation is called a detached HEAD.

2. Git Detached Head

GitHub keeps track of all commits or snapshots over time. If you check the ‘git log’ in your terminal, you can show all the previous commits up to the first commit. Detached HEAD mode allows you to discover an older state of a repository. It is a natural state in Git.

When Head doesn’t point to most recent commit, such state is called detached Head. If you checkout with an older commit, it will stand the detached head condition. See the below example:

Git Head

I have copied the older commit id. Now I will check out with this id.

Git Head

As you can see in the given example, Head does not point the most recent commit. It is called a detached head state. It is always recommended, do not commit on detached Head.