Git – Difference Between HEAD, Working Tree and Index

Git, a popular version control system, helps developers track changes and collaborate on projects efficiently. To use Git effectively, it’s essential to understand its key components: HEAD, the working tree, and the index (also known as the staging area). This article will explain these concepts and highlight their differences, helping you gain a deeper understanding of Git’s functionality.

Git as a version-control-system manages and manipulates three trees in its normal operation:

  • HEAD: Last commit snapshot, next parent
  • Index: Proposed next commit snapshot 
  • Working Directory: Sandbox

Head

HEAD is the pointer to the current branch reference, which is in turn a pointer to the last commit made on that branch. That means HEAD will be the parent of the next commit that is created. It’s generally simplest to think of HEAD as the snapshot of your last commit on that branch.

What does it contain?

Use git ls-files -s to see what it looks like. You should see something like this:

100644 a906cb2a4a904a152e80877d4088654daad0c859 0 README   
100644 8f94139338f9404f26296befa88755fc2598c289 0 Rakefile
100644 47c6340d6459e05787f644c2447d2595f5d3a54b 0 lib/simplegit.rb

Key Points about HEAD

  • Points to the Latest Commit: HEAD always points to the most recent commit in the current branch.
  • Moves with Each Commit: When you make a new commit, HEAD moves to point to the new commit.
  • Detached HEAD: When you check out a specific commit instead of a branch, you enter a “detached HEAD” state. In this state, HEAD points to a specific commit rather than the latest commit on a branch

Working Tree

This is where your files reside and where you can try changes out before committing them to your staging area (index) and then into history.

Syntax:

git status

Key Points about the Working Tree

  • Reflects Current Branch: The working tree contains the files from the branch you have checked out.
  • Editable: You can modify files in the working tree, but these changes are local until you stage and commit them.
  • Shows Untracked and Modified Files: Files in the working tree can be untracked, modified, or match the last commit.

Index (Staging Area)

The index, also known as the staging area, is an intermediate area where you prepare changes before committing them. When you stage changes, you add them to the index, making them ready for the next commit.

Key Points about the Index

  • Prepares Changes for Commit: The index holds a snapshot of your changes that will be included in the next commit.
  • Separate from Working Tree: Changes in the working tree must be explicitly staged before they move to the index.
  • Allows for Partial Commits: You can choose to stage only specific changes, enabling more precise and meaningful commits.

Let’s see how these three trees work together?

Git’s typical workflow is to record snapshots of your project in successively better states, by manipulating these three trees. Take a look at this picture:

Git Typical Workflow

To get a good visualized understanding, consider this scenario. Say you go into a new directory with a single file in it. Call this v1 of the file. It is indicated in blue. Running git init will create a Git repository with a HEAD reference that points to the unborn master branch

At this point, only the working directory tree has any content. Now we want to commit this file, so we use git add to take content in the working directory and copy it to the index.

Then we run git commit, which takes the contents of the index and saves it as a permanent snapshot, creates a commit object which points to that snapshot, and updates master to point to that commit.

Differences Between HEAD, Working Tree, and Index

The following table summarizes the differences between HEAD, the working tree, and the index:

Component Description Key Commands
HEAD Pointer to the latest commit in the current branch git log -1
Working Tree The directory where your project files are checked out git status
Index Intermediate area to prepare changes for the next commit git add <filename>, git diff --cached