GIT HEAD

Hemashree S

Git Head

  • The HEAD represents the latest commit in the currently checked-out branch and acts as a pointer to a specific reference. 
  • Essentially, HEAD indicates the "current branch," and when you switch branches using "checkout", HEAD moves to the new branch. 


  • In the figure above, the HEAD points to commit-1 because a checkout was performed at that commit. When a new commit is made, HEAD shifts to the latest commit.
  •  The git head command can be used with various arguments to check the status of HEAD. 
  • The status of HEAD is stored in the. git\refs\heads directory. Consider the example below:

Git Show Head

The "git show HEAD" command is used to view the current status and location of the HEAD.

Syntax

$ git show HEAD


Output:


The output above displays the commit ID of the HEAD, indicating that the HEAD points to that specific commit.

Next, view the project's commit history using the git log command. Refer to the output below:



As shown in the output above, the commit ID of the most recent commit matches that of the HEAD, indicating that the HEAD points to the latest commit.

You can also check the status of the HEAD using the commit ID. Copy the commit ID from the output above and use it with the git show command. If the commit ID corresponds to the latest commit, the result will be the same as the git show HEAD command. Refer to the output below:


The output above is identical to the result of the git show command.

The HEAD can point to a specific revision that is not linked to any branch name, a state known as a detached HEAD.

Git Detached Head

GitHub tracks all commits or snapshots over time. Using the git log command in your terminal, you can view all previous commits, starting from the initial one. Detached HEAD mode lets you explore an earlier state of the repository and is a standard state in Git.

When the HEAD does not point to the most recent commit, this state is known as a detached HEAD. If you check out an older commit, you will enter this detached HEAD condition. Refer to the example below:


I have copied the ID of the older commit, and now I will check out using that ID.


As shown in the example, the HEAD does not point to the most recent commit, indicating a detached HEAD state. It is generally advised not to make commits while in a detached HEAD state.

















Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send