Staging Area in Git

Azhar Bashir Khan Jun 02, 2022
Staging Area in Git

In this tutorial, we will learn about the staging area in Git.

Git, a version control system, maintains the history of the changes done to a project directory. Git uses commits to keep track of changes.

Git has three internal management systems, one of which is the staging area. The staging area is like a rough draft space where we can add the version of the files that we want to save in the next commit.

We use the git add command to add the files to the staging area. We will now illustrate this with an example.

Staging Area in Git

Git is used in a collaborative development environment to keep track of the changes done to files in the project directory.

Git has three internal management systems, also known as trees: the working directory tree, the staging index tree, and the Commit History tree.

These trees are complex data structures used to manage the state of the files and their changes in the Git repository. The staging area, also known as the staging index tree, tracks the working directory changes.

The command git add is used to copy the version of the files from your working directory to the staging area. The git add command updates the index or the staging area, using the current content found in the working tree.

The staging area holds a snapshot of the content of the working tree. This snapshot is the one that is taken as the contents of the next commit.

Later, when we want to commit the changes to the Git repository, we need to use the git commit command.

The git commit command uses the snapshot created by the git add command to create the commit in the Commit History tree. The git commit command adds the changes to a permanent snapshot that lives in the Commit History tree.

The staging area is a complex internal caching mechanism. We can view the staging area or index state using the git ls-files command.

We can run the git ls-files command in the Git repository.

$ git ls-files -s .
100644 bab2a0adb8921f504cb0521bc00b8dde22ee92a4 0	mynotes.txt

We can see that the mynotes.txt file is part of the staging area tree.

The -s or --stage option, provided to the git ls-files command, displays the additional metadata for the files in the Staging Index. The metadata is the staged contents’ mode bits, object name, and stage number.

The second value bab2a0adb8921f504cb0521bc00b8dde22ee92a4 is a standard Git object SHA-1 hash. It is the hash of the content of the files.

We can add the modifications of the mynotes.txt file to the staging area by using the git add command.

$ git add mynotes.txt
$ git status
On branch main Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified: mynotes.txt

As shown in the output of the git status command, the modifications to the file mynotes.txt are promoted to the Staging Index by the git add command.

We will again check the status of the staging area or index for the mynotes.txt file as follows.

$ git ls-files -s mynotes.txt
100644 067478ae06e267263ea7ed849ef358f911628668 0	mynotes.txt

We can see that the object SHA for the mynotes.txt file has been updated from bab2a0adb8921f504cb0521bc00b8dde22ee92a4 to 067478ae06e267263ea7ed849ef358f911628668.

Thus, we have learned about the staging area tree or index in Git.

For more information, please visit:

  1. git-add
  2. Saving changes

Related Article - Git Add