The Staging Area (Git Index)
After creating a Git repository, you can modify several files in the project’s working tree.
However, you may not want every modification to become part of the next commit.
Git provides an intermediate preparation area called the Staging Area.
The Staging Area is the preparation list for the next commit. It lets you choose which working-tree changes should be included.
The Staging Area and the Index
Section titled “The Staging Area and the Index”The Staging Area is also called the Git Index.
These two terms refer to the same concept:
Staging Area = Git IndexIt represents the state that Git is preparing to use for the next commit.
Why Do We Need a Staging Area?
Section titled “Why Do We Need a Staging Area?”Imagine that you modified three files:
auth.jsfeature.jsREADME.mdThe three modifications currently exist in the Working Tree.
However, suppose only the change in auth.js is complete and ready to be
recorded. The changes in feature.js and README.md are still unfinished.
The Staging Area allows you to select auth.js without including the other two
files.
Selecting a File with git add
Section titled “Selecting a File with git add”To place the current content of auth.js in the Staging Area, run:
git add auth.jsThe state now looks like this:
Working Tree
- auth.jsmodified
- feature.jsmodified
- README.mdmodified
All three files are modified on disk.
Staging Area (Git Index)
- auth.jsstaged
Only the content of auth.js is prepared for the next commit. feature.js and README.md stay unstaged.
Only auth.js has been prepared for the next commit.
The modifications in feature.js and README.md still exist on disk in the
Working Tree, but they are not part of the prepared snapshot.
What Does git add Select?
Section titled “What Does git add Select?”git add auth.js does not merely mark the file name.
It places the current content of auth.js into the Git Index as the version
prepared for the next commit.
If you later modify auth.js again, the new modification is not automatically
added to the existing staged version. You would need to stage the file again if
you wanted the newer content included.
What Will the Commit Include?
Section titled “What Will the Commit Include?”When a commit is created, Git records the state prepared in the Staging Area.
In this example:
Staged: auth.js
Not staged: feature.js README.mdTherefore, the next commit includes the staged version of auth.js.
The unstaged modifications in feature.js and README.md remain in the Working
Tree and are not included.
The Shopping-Cart Analogy
Section titled “The Shopping-Cart Analogy”Think of the Staging Area as a shopping cart for the next snapshot.
The store contains many available items, but only the items you deliberately place in the cart will be included when you check out.
In the same way:
Working Tree → All current modificationsStaging Area → Changes selected for the next commitCommit → Records the prepared selectionIn our example:
Shopping cart:✓ auth.js
Still outside the cart:- feature.js- README.mdThe Staging Area gives you control over what belongs together in the next recorded checkpoint.
The Three Areas in Git
Section titled “The Three Areas in Git”Every normal Git project has three important areas.
Understanding these areas makes the Git workflow easier to follow:
Working Tree
Edit project files.
Edit and save
Staging Area
Select what belongs in the next commit.
git add
Repository
Store recorded snapshots.
git commit
Each area has a different responsibility.
1. Working Tree
Section titled “1. Working Tree”The Working Tree, also called the Working Directory, contains the visible project files you open and edit.
It is your daily workspace.
When you edit and save a file, the new content exists in the Working Tree. Saving the file does not automatically prepare it for a commit or add it to Git history.
Working TreePurpose: Edit project filesAction: Edit and save2. Staging Area
Section titled “2. Staging Area”The Staging Area, also called the Git Index, is the preparation area for the next commit.
You use git add to select the current content that should be included.
Staging AreaPurpose: Select what belongs in the next commitAction: git addYou do not have to stage every modified file.
For example, if you modified:
auth.jsfeature.jsREADME.mdbut only auth.js is ready, you can stage only that file:
git add auth.jsThe other modifications remain in the Working Tree and do not become part of the next commit.
3. Repository
Section titled “3. Repository”The Repository contains the project’s recorded history inside .git.
When you create a commit, Git records a new snapshot based on the state currently prepared in the Staging Area.
RepositoryPurpose: Store recorded snapshotsAction: git commitExample: One File Journey
Section titled “Example: One File Journey”Let us follow auth.js through the three areas of Git. The file does not
physically move between folders. Instead, Git keeps track of its state in each
area.
Moment 1
Create the file
Working Tree: auth.js exists as an untracked file.
Staging Area: empty.
Repository: no snapshot of the file exists yet.
Moment 2
git add auth.js
Working Tree: still contains auth.js.
Staging Area: contains the current version of auth.js.
Repository: still has no snapshot of the file.
Moment 3
git commit -m “fix login”
Working Tree: matches the committed version.
Staging Area: no pending staged changes.
Repository: contains the new snapshot.
Moment 4 Now
Edit the file again
Working Tree: contains new modified content.
Staging Area: does not contain the new modification.
Repository: still contains the version saved by the last commit.
1. Create the file
Section titled “1. Create the file”We create and save auth.js in the project directory.
- Working Tree:
auth.jsexists as an untracked file. - Staging Area: Empty.
- Repository: No snapshot of the file exists yet.
At this moment, the file exists only in the Working Tree. Git can see it, but it has not been selected for the next commit.
2. Stage the file
Section titled “2. Stage the file”git add auth.js- Working Tree: Contains
auth.js. - Staging Area: Contains the current version of
auth.js. - Repository: Still has no snapshot of the file.
git add copies the current state of auth.js into the Staging Area, preparing
that exact version for the next commit.
3. Commit the staged version
Section titled “3. Commit the staged version”git commit -m "fix login"- Working Tree: Matches the committed version.
- Staging Area: No pending staged changes.
- Repository: Contains the new snapshot.
git commit saves the version prepared in the Staging Area as a permanent
snapshot in the repository history.
4. Edit the file again
Section titled “4. Edit the file again”We make another change to auth.js and save it.
- Working Tree: Contains new modified content.
- Staging Area: Does not contain the new modification.
- Repository: Still contains the version saved by the last commit.
The new edit is not automatically staged. To include it in another commit, we
must run git add auth.js again before creating the next commit.
The important idea:
git addstages the file’s current version at that moment. If you edit the file afterward, the new changes remain in the Working Tree until you stage them again.
How Changes Move
Section titled “How Changes Move”The normal movement is:
1. Edit or save files Working Tree
2. Select the required content Working Tree → git add → Staging Area
3. Record the prepared state Staging Area → git commit → RepositoryThis separation gives you control over the contents of each commit.
You Do Not Have to Commit Everything
Section titled “You Do Not Have to Commit Everything”Suppose you are working on two different tasks:
- Fixing the login form.
- Updating the README documentation.
Both changes may exist in the Working Tree, but you may want to record the login fix first.
You can prepare only the login-related file for the next commit. The README modification remains safely in the Working Tree for later.
Conceptually:
Working Tree:- auth.js modified- README.md modified
Staging Area:- auth.js selected
Next commit:- Includes the staged state of auth.js- Does not include the unstaged README.md modificationThis allows one commit to represent one focused piece of work.
Updated Key Takeaway
Section titled “Updated Key Takeaway”The three Git areas work together:
- Working Tree: where you edit and save visible project files.
- Staging Area: where you select the content for the next commit.
- Repository: where commits record the prepared snapshots in Git history.
The workflow is:
Edit → Stage the required changes → Commit the staged stateYou decide which modifications enter the Staging Area. Therefore, a commit can contain only the files relevant to one task while other unfinished modifications remain in the Working Tree.