Staging Area
7.1 What is the Staging Area?
When you modify files in your project:
1. They start in the working directory (untracked or modified).
2. You add specific files to the staging area using git add.
3. You finalize them into the repository with git commit.
This gives you granular control over what goes into each commit.
Visualized:
1. They start in the working directory (untracked or modified).
2. You add specific files to the staging area using git add.
3. You finalize them into the repository with git commit.
This gives you granular control over what goes into each commit.
Visualized:
1Working Directory → Staging Area → Repository
2(untracked files) (selected changes) (committed history)7.2 Adding Files to the Staging Area
You can stage individual files or all changes at once.
Stage a single file:
Stage all modified files:
Stage specific file patterns:
This makes only selected files part of the next commit — useful when you're working on multiple things at once but want clean, focused commits.
Stage a single file:
1git add index.htmlStage all modified files:
1git add .Stage specific file patterns:
1git add *.jsThis makes only selected files part of the next commit — useful when you're working on multiple things at once but want clean, focused commits.
7.3 Checking the Staging Area Status
Use:
You'll see three categories:
• Untracked files: new files not staged yet
• Changes not staged for commit: modified but unstaged
• Changes to be committed: staged and ready for commit
You can also preview exact staged changes:
You can also preview exact staged changes:
1git statusYou'll see three categories:
• Untracked files: new files not staged yet
• Changes not staged for commit: modified but unstaged
• Changes to be committed: staged and ready for commit
You can also preview exact staged changes:
You can also preview exact staged changes:
1git diff --staged7.4 Removing Files from the Staging Area
To unstage a file (move it back to the working directory):
Example:
This keeps your file changes in the working directory but removes them from the next commit — perfect if you added something by mistake.
1git restore --staged <filename>Example:
1git restore --staged main.pyThis keeps your file changes in the working directory but removes them from the next commit — perfect if you added something by mistake.
7.5 Why the Staging Area Matters
• Selective Commits: You can commit only specific changes rather than everything modified.
• Review Before Commit: Ensures you don't accidentally save debug code or temporary edits.
• Organized History: Enables clean commit messages tied to logical feature units.
Without the staging area, every change would go directly into commits — making history messy and harder to manage.
• Review Before Commit: Ensures you don't accidentally save debug code or temporary edits.
• Organized History: Enables clean commit messages tied to logical feature units.
Without the staging area, every change would go directly into commits — making history messy and harder to manage.
7.6 Useful Commands Recap
| Action | Command |
|---|---|
| Add a file to staging | git add <file> |
| Add all changes | git add . |
| See staged files | git status |
| See staged diff | git diff --staged |
| Unstage a file | git restore --staged <file> |
7.7 Key Takeaways
• The staging area is Git's "review zone" — where you prepare what goes into your next commit.
• Use git add to stage and git restore --staged to unstage changes.
• Always run git status before committing to confirm what's being saved.
• A clean, deliberate staging process keeps your Git history readable and professional.
• Use git add to stage and git restore --staged to unstage changes.
• Always run git status before committing to confirm what's being saved.
• A clean, deliberate staging process keeps your Git history readable and professional.