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
Syntax
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.
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.