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.
- 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 Hunk | Action Performed |
|---|
| y | Stage this hunk for the next commit |
| n | do not stage this hunk for the next commit |
| q | quit; do not stage this hunk or any of the commits |
| a | stage this hunk and all later hunks in the file |
| d | do not stage this hunk or any of the later hunks in the file |
| e | manually 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
What is the primary purpose of the Staging Area in Git?
-
To store remote repository data
-
To permanently save snapshots of code
-
To prepare and review changes before committing
-
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?
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?
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?
Explanation:
git add -p lets users stage code hunk by hunk, helpful for partial commits.
What does git rm --cached file_name do?
-
Deletes file permanently from system
-
Removes file from staging and filesystem
-
Removes file from Git tracking but keeps it on disk
-
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 >
Explore
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git
Advanced Git Commands