Open In App

Staging in Git

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Git, the staging area (also called the index) is an intermediate space where changes are gathered before they are committed. Think of it as a draft board: you can organize and review what changes you want to include in the next commit.

The staging area allows you to:

  • Selectively choose changes to commit.
  • Break down large changes into smaller, logical commits.
  • Review your work before finalizing it in the repository.
Staging
  • Working Directory: Contains your local files and changes (untracked or modified).
  • git add: Moves selected changes from Working Directory to Staging Area for preparation.
  • Staging Area: Holds files ready to be committed (also called index).
  • git commit: Saves the staged changes as a snapshot to the Repository.
  • Repository: Stores the full history of committed changes (local Git repo).

Basic Staging Commands

1. Staging All Changes

To stage all modified files in your working directory, use:

git add .

Alternatively, you can use:

git add -A

This adds all changes, including new, modified, and deleted files.

2. Staging Changes in a File

Let’s say we edit a.txt and run:

git status
  • Changes appearing in green indicate the file is staged but not yet committed.
  • Changes appearing in red are not staged yet.

Example:

  • You write "hello" in a.txt and stage it using git add ..
  • Then you add "bhailogs" but don’t stage it. Running git status shows "bhailogs" in red, indicating unstaged changes.

3. Unstage a File

To remove a file from the staging area, use:

git reset file_name

Example:

  • You staged b.txt but now want to unstage it:
git reset b.txt

The file remains in your working directory but is removed from staging.

3. Staging Changes by Hunk

Sometimes you want to stage only parts of a file. Git allows hunk-level staging:

git add -p

This opens an interactive prompt for each hunk (block of changes) in the file:

Stages of HunkAction Performed
yStage this hunk for the next commit
ndo not stage this hunk for the next commit
qquit; do not stage this hunk or any of the commits
astage this hunk and all later hunks in the file
ddo not stage this hunk or any of the later hunks in the file
emanually edit the current hunk
?print hunk help

This is useful when you want to commit only specific changes while leaving other changes unstaged.

4. Interactive Add

Git also provides an interactive staging interface:

git add -i

This interface breaks down the output into staged and unstaged changes. You can perform various actions:

  • status: Shows the current state of files.
  • update: Stage modified files.
  • revert: Revert changes back to HEAD.
  • add untracked: Stage untracked files.
  • patch: Stage individual hunks interactively.
  • diff: Shows changes to be committed.
  • quit: Exit the interactive interface.
  • help: Displays help for interactive commands.

Staging Specific Cases

1. Stage a Single File

git add file_name

Example:

git add b.txt

2. Stage Deleted Files

  • To remove a file permanently from Git:
git rm -f file_name

To remove a file from Git without deleting it from disk:

git rm --cached file_name

Example:

  • You create r.txt, stage it, and then remove it using --cached. The file is removed from staging but remains in your working directory as untracked. You can stage it again using:
git add r.txt


Suggested Quiz
5 Questions

What is the primary purpose of the Staging Area in Git?

  • A

    To store remote repository data

  • B

    To permanently save snapshots of code

  • C

    To prepare and review changes before committing

  • D

    To delete untracked files

Explanation:

The staging area (index) is an intermediate zone where changes are gathered for review before committing them to the repository.

Which command stages all modified, new, and deleted files?

  • A

    git add file_name

  • B

    git add .

  • C

    git add -A

  • D

    git reset file_name

Explanation:

git add -A stages all changes (new, modified, deleted), while git add . stages new and modified files in current directory.

Which command is used to unstage a file without removing changes from working directory?

  • A

    git rm file_name

  • B

    git reset file_name

  • C

    git revert file_name

  • D

    git clean -f

Explanation:

git reset file_name removes file from staging area but keeps its modifications, allowing re-staging later.

Which command is used to stage changes interactively by selecting hunks?

  • A

    git add -p

  • B

    git add -i

  • C

    git commit -p

  • D

    git reset -p

Explanation:

git add -p lets users stage code hunk by hunk, helpful for partial commits.

What does git rm --cached file_name do?

  • A

    Deletes file permanently from system

  • B

    Removes file from staging and filesystem

  • C

    Removes file from Git tracking but keeps it on disk

  • D

    Stages file for commit

Explanation:

git rm --cached untracks and removes file from staging/index without deleting it locally, making it an untracked file.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore