Git Head

  • Post author:
  • Post category:Git
  • Post comments:1 Comment
Git Head

In this guide, we will discuss 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 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:

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  

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 the 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 the git show head command if the commit id is the last commit’s id. See the below output:

Git Head

The above output is the same as the 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.

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 the 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 to the most recent commit. It is called a detached head state. It is always recommended, do not to commit to a detached Head.

Next Topic: Click Here

This Post Has One Comment

Leave a Reply