Concept of Git Index and Git Head

Last updated on May 28 2022
Meetesh Jain

Table of Contents

Concept of Git Index and Git Head

Git Index

The Git index is a staging area between the working directory and repository. It is used to build up a set of changes that you want to commit together. To better understand the Git index, then first understand the working directory and repository.

Concept of Git Index and Git Head
Concept of Git Index and Git Head

There are three places in Git where file changes can reside, and these are working directory, staging area, and the repository. To better understand the Git index first, let’s take a quick view of these places.

Working directory:

When you worked on your project and made some changes, you are dealing with your project’s working directory. This project directory is available on your computer’s filesystem. All the changes you make will remain in the working directory until you add them to the staging area.

Staging area:

The staging area can be described as a preview of your next commit. When you create a git commit, Git takes changes that are in the staging area and make them as a new commit. You are allowed to add and remove changes from the staging area. The staging area can be considered as a real area where git stores the changes.
Although, Git doesn’t have a dedicated staging directory where it can store some objects representing file changes (blobs). Instead of this, it uses a file called index.

Repository:

In Git, Repository is like a data structure used by GIt to store metadata for a set of files and directories. It contains the collection of the files as well as the history of changes made to those files. Repositories in Git is considered as your project folder. A repository has all the project-related data. Distinct projects have distinct repositories.
You can check what is in the index by the git status command. The git status command allows you to see which files are staged, modified but not yet staged, and completely untracked. Staged files mean, it is currently in the index. See the below example.

Syntax:

1. $ git status

Output:

Concept of Git Index and Git Head
Concept of Git Index and Git Head

In the given output, the status command shows the index.
As we mentioned earlier index is a file, not a directory, So Git is not storing objects into it. Instead, it stores information about each file in our repository. This information could be:

• mtime: It is the time of the last update.
• file: It is the name of the file.
• Wdir: The version of the file in the working directory.
• Stage: The version of the file in the index.
• Repo: The version of the file in the repository.

And finally, Git creates your working directory to match the content of the commit that HEAD is pointing.

Git Head

The HEAD points out the last commit in the current checkout branch. It is like a pointer to any reference. The HEAD can be understood as the “current branch.” When you switch branches with ‘checkout,’ the HEAD is transferred to the new branch.

Concept of Git Index and Git Head
Concept of Git Index and Git Head

The above fig shows the HEAD referencing commit-1 because of a ‘checkout’ was done at commit-1. When you make a new commit, it shifts to the newer commit. The git head command is used to view the status of Head with different arguments. It stores the status of Head in .git\refs\heads directory. Let’s see the below example:

Git Show Head

The git show head is used to check the status of the Head. This command will show the location of the Head.

Syntax:

1. $ git show HEAD

Output:

Concept of Git Index and Git Head
Concept of Git Index and Git Head

In the above output, you can see that the commit id for the Head is given. It means the Head is on the given commit.

Now, check the commit history of the project. You can use the git log command to check the commit history. See the below output:

Concept of Git Index and Git Head
Concept of Git Index and Git Head

As we can see in the above output, the commit id for most recent commit and Head is the same. So, it is clear that the last commit has the Head.

We can also check the status of the Head by the commit id. Copy the commit id from the above output and paste it with the git show command. Its result is same as git show head command if the commit id is last commit’s id. See the below output:

Concept of Git Index and Git Head
Concept of Git Index and Git Head

The above output is the same as git show output.

The HEAD is capable of referring to a specific revision that is not associated with a branch name. This situation is called a detached HEAD.

Git Detached Head

GitHub keeps track of all commits or snapshots over time. If you check the ‘git log’ in your terminal, you can show all the previous commits up to the first commit. Detached HEAD mode allows you to discover an older state of a repository. It is a natural state in Git.

When Head doesn’t point to most recent commit, such state is called detached Head. If you checkout with an older commit, it will stand the detached head condition. See the below example:

Concept of Git Index and Git Head
Concept of Git Index and Git Head

I have copied the older commit id. Now I will check out with this id.

Concept of Git Index and Git Head
Concept of Git Index and Git Head

As you can see in the given example, Head does not point the most recent commit. It is called a detached head state. It is always recommended, do not commit on detached Head.

So, this brings us to the end of blog. This Tecklearn ‘Concept of Git Index and Git Head’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Git and GitHub and build a career in DevOps domain, then check out our interactive, Version control with Git and GitHub Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Version Control with Git and GitHub

Version Control with Git and GitHub Training

About the Course

Tecklearn has specially designed this Git and GitHub Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of important concepts like: branching & merging, how to deal with conflicts, rebasing, merge strategies, Git workflows and so on. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Git.

Why Should you take Git and GitHub Training?

• Average salary of Git and GitHub Professional is $85k – Indeed.com
• Amazon, Google, Facebook, Microsoft, Twitter, & many other MNC’s worldwide use Git across industries.
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation

Git and GitHub

• What is version control
• Version Control System (VCS) Products
• Types of VCS
• What is Git
• Why Git for your organization
• Install Git
• Common commands in Git
• Working with Remote Repositories
• GitHub
• Git Installation
• Git Lifecycle
• GitHub (Push, Pull Request)
• GitHub Workflow

 

0 responses on "Concept of Git Index and Git Head"

Leave a Message

Your email address will not be published. Required fields are marked *