# What Is Head in Git?

In Git, `HEAD` is a special reference that points to the current branch or commit you are working on. It plays a crucial role in various Git operations and helps Git keep track of the current position in your repository.

### Understanding `HEAD`

Here’s a detailed breakdown of what `HEAD` represents and how it is used in Git:

### 1. **Pointer to the Current Branch**

- **Current Branch Reference**: When you are working on a branch (e.g., `master`, `feature-branch`), `HEAD` points to the latest commit on that branch. This helps Git determine where you are in the commit history.
    
    For example, if you are on the `feature-branch`, `HEAD` points to the tip of the `feature-branch`, which in turn points to the latest commit on that branch.
    
    ```
    git branch
    ```
    
    The output will show an asterisk (`*`) next to the current branch:
    
    ```
    * feature-branch
      master
    ```
    
    In this case, `HEAD` is pointing to `feature-branch`.
    

### 2. **Pointer to a Commit**

- **Detached HEAD**: When you check out a specific commit (e.g., `git checkout <commit-hash>`), `HEAD` points directly to that commit rather than a branch. This is known as a "detached HEAD" state. You are no longer working on a branch, but rather on a specific commit.
    
    ```
    git checkout <commit-hash>
    ```
    
    In this state, you can still make changes and create new commits, but these commits will not belong to any branch. To save these changes, you would need to create a new branch from this state.
    

### 3. **Pointer to a Branch Reference**

- **Branch Pointer**: `HEAD` also points to a branch reference, which in turn points to the latest commit on that branch. This is crucial for operations like commits and merges because Git uses `HEAD` to determine where changes should be applied.

### 4. **Resetting `HEAD`**

- **Changing `HEAD`**: You can use various Git commands to change where `HEAD` points. For example:
    - **Checkout**: Moves `HEAD` to a different branch or commit.
        
        ```
        git checkout branch-name
        ```
        
    - **Reset**: Moves `HEAD` to a different commit and optionally updates the working directory and index.
        
        ```
        git reset --hard <commit-hash>
        ```
        
    - **Rebase**: Moves `HEAD` and re-applies commits on top of a new base commit.

### 5. **`HEAD` in Git Internals**

- **File Location**: Internally, `HEAD` is stored in a file called `HEAD` located in the `.git` directory of your repository. This file contains a reference to the current branch or commit.
    
    ```
    .git/HEAD
    ```
    
    The content of this file typically looks like:
    
    ```
    ref: refs/heads/feature-branch
    ```
    
    This indicates that `HEAD` is pointing to the `feature-branch`.
    

### Summary

- **`HEAD`** is a special reference in Git that points to the current branch or commit you are working on.
- **Current Branch**: When you are on a branch, `HEAD` points to the latest commit on that branch.
- **Detached HEAD**: When you check out a specific commit, `HEAD` points directly to that commit, not a branch.
- **Git Commands**: Various commands like `checkout`, `reset`, and `rebase` modify where `HEAD` points.
- **File Location**: Internally, `HEAD` is stored in the `.git/HEAD` file.

Understanding `HEAD` is essential for navigating and manipulating your Git repository effectively.