Git

What’s the Difference Between HEAD, Working Tree, and Index in Git?

Git is a distributed version control system that tracks the difference between the working directory and the local Git repository, similarly between the Git local and remote repositories. While working on Git, developers deal with a large number of files. Initially, they work on the working directory then they move their files from the working directory to the Git index. After that, they commit changes to save data to the local repository. So, HEAD moves with every new commit.

This post will differentiate the HEAD, working tree, and index in Git.

What’s the Difference Between Working Tree, HEAD, and Index in Git?

HEAD” is a unique reference that points to the branch or commits in which the users are currently working. The “Working tree” is the current working area on which the users work that holds all the unstaged changes. Whereas “Index” is the staging area between the working directory and the local repository that contains the changes which need to be committed.

How to Find HEAD Pointer in Git?

In order to view the current position of HEAD, utilize the “git log” command along with the “–oneline” option:

$ git log --oneline

The below output indicates that the HEAD is pointing to the “master” branch and “d3fd3b” commit:

How to Find Working Tree in Git?

If developers want to view the list of all untracked changes from the working tree, it is required to execute the “git ls-tree HEAD” command:

$ git ls-tree HEAD

According to the below-stated output:

  • First column represents the permissions of files(read-write).
  • Second column shows “blob”, which is a type of object that stands for a large binary object used to store the contents of each file in a repository.
  • Third column holds the commit id of the current working repository commits.
  • Fourth column contains the list of the files’ titles.

How to Find Index in Git?

To find the index in Git, run the “git ls-files” command:

$ git ls-files -s

In the below output:

  • -s” flag is used for the staged files.
  • Column 1 indicates the file chmod or permissions.
  • Column 2 contains the SHA-hash of current working repository commits.
  • Similarly, column 3 represents the index of all files that is “0”.
  • The last column shows the list of available files’ titles.

We have differentiated between HEAD, working tree, and Index in Git.

Conclusion

HEAD is a pointer that determines the branch or commits that the user last checked out. A working tree is a current place where the user works and keeps files. However, the Index is a Git staging area where users commit new changes. This post demonstrated the difference between HEAD, working tree, and Index.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.