Git - Head



HEAD is a reference to the most recent commit on the branch that is being worked on in Git.

It can be seen as a marker or pointer pointing at the active branch's tip.

HEAD essentially keeps track of the commit that is being worked on and adjusts when branch switching or examining various commits.

  • HEAD is updated to point to the tip of the new branch when the checkout command is used to switch branches.

  • As a result, HEAD becomes a dynamic reference that aids in monitoring the repository's current working context.

  • As the most recent commit on the current branch, HEAD is essential for comprehending and navigating the status of a Git repository.

Head in the Git

Pointer to Current Commit

In Git, HEAD serves mainly as a pointer to the most recent commit made to the branch that is being worked on right now.

  • Git is able to monitor the project's current status and determine which changes are ready for the upcoming commit because of this.

  • It guarantees that all work originates from the most recent point in the branch.

Detached Head

HEAD may become detached while examining a remote branch, a tag, or an older commit.

  • Instead of referring to a branch in this case, HEAD points straight to a particular commit.

  • Unless a new branch is created from the detached HEAD, any new commits made in this state will not be connected with any branch and will be lost if switching back to a branch.

Moving HEAD

Several Git functions, such as git checkout, git switch, and git reset, change HEAD dynamically.

  • By changing the current branch or reverting changes, these instructions cause HEAD to redirect to the correct commit.

  • For instance, git reset can roll back changes by shifting HEAD to a prior commit, git switch allows branch switching, and git checkout modifies the working branch.

Understanding the concepts of refs and HEAD is crucial for understanding the management of branches and commits in Git.

Git Refs

Refs are pointers to specific commits.The files are kept in the .git/refs directory and arranged into tags and heads, among other subdirectories.

Heads: This subdirectory contains references to the latest commit of each branch.

For example, the file named after a branch (e.g., master or main) holds the commit ID of the latest commit on that branch.

Tags: This subdirectory contains references to specific commits associated with tags.

HEAD

The unique reference HEAD points to the branch we are now working on and, consequently, to the most recent commit made to that branch.

  • The branch that is presently checked out can be seen in the .git/HEAD file.

  • For example, the .git/HEAD file will point to refs/heads/master if we are on the master branch.

  • HEAD is updated to refer to the most recent commit of the new branch when we switch branches.

  • This facilitates the management of our working directory's state by Git with respect to the branch we are on.

The HEAD reference's current state can be seen by using the git show HEAD command.

  • It indicates the commit that HEAD is pointing to and gives information about it, including the commit message, author, date, and modifications done.

  • The location of HEAD in respect to the most recent commit on the current branch is also displayed by this command.

$ git show HEAD

Git Detached Head

A detached HEAD state occurs when checking out a specific commit directly instead of a branch.

HEAD does not point to a branch reference, instead, it points directly to the specified commit.

Enter this state using the command:

git checkout <commit ID>

In a detached HEAD state, new commits are not associated with any branch unless a new branch is created from this point.

This state is useful for inspecting or working with the repository at a particular commit without affecting branch structures.

To revert changes to a specific file from an older commit, use:

    
git checkout <commit ID> <file>

Several specific references related to HEAD are frequently used in sophisticated Git workflows.

  • ORIG_HEAD: This monitors HEAD before significant actions such as git reset.

    It's helpful for going back to the initial configuration in case something goes wrong.

  • MERGE_HEAD: This indicates that, during a merge operation, the commit or commits are merged into the current branch.

    Git uses it to monitor and control the merging process.

  • FETCH_HEAD: The most recent commit that was retrieved from a remote repository is indicated by this.

    It indicates the location of the latest fetch operation's tip.

Using Git Commands with Head

Using HEAD to reset: By reverting HEAD to the previous commit, the command git reset HEAD~1 undoes the most recent commit.

In simple terms, this reverses the most recent commit.

Referencing Previous Commits:

  • HEAD~1 refers to the commit directly before the current one.

  • HEAD~2 refers to two commits before the current one.

This way, HEAD can be used to easily navigate and reference previous commits in the history.

In this scenario, we need to review a version of the project from four commits ago and possibly start a new branch from that point.

View the Commit Log

First, check the commit history to identify where HEAD is currently pointing and find the commit of interest:

git log --oneline

b2c3d4e (HEAD -> main) Implement user authentication
a4b5c6d Improve error handling
c7d8e9f Refactor database schema
d0e1f2g Update dependencies
e3f4g5h Initial commit

Here, HEAD is pointing at commit b2c3d4e with the message Implement user authentication.

Check Out an Older Commit

To examine the project state from four commits ago, use the command:

git checkout HEAD~4

Note: switching to 'e3f4g5h'.

You are in 'detached HEAD' state. You can explore this state, make experimental changes, and create commits without affecting branches. 
Switch back to a branch when done to avoid losing work.

This message indicates that HEAD is now pointing to commit e3f4g5h, and the repository is in a detached HEAD state.

Create a New Branch

If this older commit is a good starting point for new work, create a new branch from this detached HEAD state:

git checkout -b feature-old-state

Switched to a new branch 'feature-old-state'

HEAD is now pointing to the new branch feature-old-state, starting from commit e3f4g5h.

Make Changes and Commit

Make any required changes, then add and commit them:

git add .
git commit -m "Begin new feature development based on earlier commit"

[feature-old-state f3g4h5i] Begin new feature development based on earlier commit
1 file changed, 2 insertions(+)

HEAD has moved to the new commit f3g4h5i on the feature-old-state branch.

Verify the Current HEAD

To confirm where HEAD is now pointing after the new commit, use:

git show-ref HEAD

f3g4h5i1a2b3c4d5e6f7g8h9i0jklmnopqrstu refs/heads/feature-old-state
Advertisements