Open In App

Git – Difference Between HEAD, Working Tree and Index

Last Updated : 28 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

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.

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.

If we run git status, we’ll see no changes, because all three trees are the same.

Tip: Do remember certain keypoint as follows: 

git status shows the difference between these trees in the following manner:

  • If the Working Tree is different from the index, then git status will show there are some changes not staged for commit
  • If the Working Tree is the same as index, but they are different from HEAD, then git status will show some files under changes to be committed section in its result
  • If the Working Tree is different from the index, and the index is different from HEAD, then git status will show some files under changes not staged for commit section and some other files under changes to be committed section in its result.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads