Skip to main content

Command Palette

Search for a command to run...

Git HEAD

Published
2 min readView as Markdown
Git HEAD

Introduction

In Git, HEAD is a symbolic reference that points to the tip of the current branch, which in turn points to the most recent commit on that branch. It represents the snapshot of your project that is currently checked out in your working directory.

Key aspects of HEAD:

  • Current Commit Pointer:

    HEAD always indicates the commit you are currently working on. When you create a new commit, HEAD automatically updates to point to this new commit.

  • Default State (Attached HEAD):

    In its default state, HEAD is "attached" to a branch. This means any changes you commit will be added to that specific branch, and HEAD will move along with the branch tip.

  • Detached HEAD:

    You can enter a "detached HEAD" state by checking out a specific commit hash directly (e.g., git checkout <commit-hash>). In this state, HEAD points directly to a commit, not a branch. Changes made and committed in a detached HEAD state will not be part of any existing branch unless you explicitly create a new branch from that commit.

  • Referencing Previous Commits:

    HEAD can be used with tilde (~) or caret (^) notation to refer to parent or ancestor commits:

    • HEAD~n: Refers to the commit n commits before HEAD in the commit history. For example, HEAD~1 is the parent of the current commit.
    • HEAD^n: Refers to the n-th parent of HEAD. This is primarily used with merge commits, which can have multiple parents. HEAD^ is equivalent to HEAD^1.

Common uses of HEAD in Git commands:

  • git log HEAD: Shows the commit history starting from the current HEAD.
  • git reset HEAD~1: Undoes the last commit by moving HEAD back one commit.
  • git checkout HEAD~2 <file>: Checks out a specific file as it existed two commits ago.
  • git show HEAD: Displays details of the commit HEAD is currently pointing to.

Conclusion

In Git, HEAD is a crucial concept that you need to understand and it represents the current state of your local repository. It functions as a pointer to the most recent commit on the branch that is currently checked out.