DevOps · Infrastructure as Code (IaC) · Git Series – Part 5
Git staging (also called the index or cache) is one of Git’s most powerful features — but also one of the most misunderstood.
In this chapter, you’ll learn:
- What the staging area is
- Why Git uses it
- How to stage files
- How partial staging works
- How to unstage changes
- How Git stores staged content
- Real-world examples
Table of Contents
This knowledge is essential for becoming a confident Git user.
1. What Is the Staging Area?
The staging area is a preview zone where you place the changes you want to include in the next commit.
Think of it like a shopping cart:
- You make changes → they stay in “modified” state
- You choose which ones to stage → they go into “staging area”
- You commit → only staged items are saved
This separation gives Git precision and safety.
2. Why Git Has a Staging Area
The staging area allows you to:
✔ Commit only selected changes (not everything in the repo)
✔ Combine changes from multiple files into a single commit
✔ Create clean, meaningful commit histories
✔ Avoid accidental commits
✔ Review what will be committed before finalizing
Without staging, every modified file would be committed automatically — messy and error-prone.
3. Checking What’s Staged vs Modified
Run:
git status
Git separates changes into two sections:
Changes to be committed: (STAGED)
Changes not staged for commit: (MODIFIED)
This lets you understand what will and will not go into the next commit.
4. How to Stage Changes
Stage a single file
Stage a specific file to prepare it for the next commit.
git add file.txtStage multiple files
Add multiple files at once by specifying their names or patterns.
git add file1.txt file2.txtStage an entire folder
Stage all files within a directory and its subdirectories recursively.
git add src/Stage everything
Stage all tracked and new files in the repository at once.
git add .Stage modified files only
Stage only changes to already-tracked files, excluding untracked files.
git add -u5. How to Unstage Changes
Remove a file from staging
Remove a specific file from the staging area without losing the changes.
git restore --staged file.txtUnstage everything
Clear the entire staging area, returning all files to modified state.
git restore --staged .Legacy command (still works):
git reset HEAD file.txt6. Partial Staging (The Most Powerful Feature)
You can commit only part of a file, not the whole thing.
This is useful when:
- You fixed two unrelated issues in the same file
- You want clean, focused commits
- You accidentally put too many changes together
Interactive hunk staging
git add -p file.txtGit will show chunks (“hunks”) and ask:
Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?Common options:
y→ stage this hunkn→ skipa→ stage all remainingd→ skip alle→ edit the hunk manually
This gives you pinpoint control over your commits.
7. Staging Deleted Files
Git automatically recognizes deleted files.
To stage deletions:
rm oldfile.txt
git add -uor
git rm oldfile.txt8. Viewing What’s Actually Staged
Show the exact staged changes:
git diff --cachedShow unstaged changes:
git diff9. Real Hands-On Example
Start fresh:
mkdir staging-demo
cd staging-demo
git initCreate a file:
echo "Line 1" > demo.txt
git add demo.txt
git commit -m "Initial commit"Modify the file:
echo "Line 2" >> demo.txt
echo "Line 3" >> demo.txtCheck status:
git statusStage only part of the file:
git add -p demo.txt
Commit:
git commit -m "Added Line 2 only"Remaining changes still show as modified.
10. Git Staging Best Practices
✔ Stage intentionally, not automatically
Avoid committing random leftovers.
✔ Use git diff and git diff --cached
Review staging carefully.
✔ Use partial staging for clean commit history
Keep commits small and focused.
✔ Avoid staging generated files
Use .gitignore to prevent clutter.
✔ Use meaningful commit messages
The staging area makes commit separation easy — use it well.
11. Summary Table – Staging Commands
| Action | Command |
|---|---|
| Stage file | git add file.txt |
| Stage all | git add . |
| Stage modified only | git add -u |
| Unstage file | git restore --staged file.txt |
| View staged changes | git diff --cached |
| Partial staging | git add -p file.txt |
| Remove tracked file | git rm file.txt |
12. What’s Next?
You now understand:
✔ What staging is
✔ How to stage files
✔ How to unstage files
✔ Partial staging
✔ How Git stores staged content
✔ Best practices
Next chapter:
➡️ Part 6 – Git Commit: Creating Effective, Clean, and Professional Commits
Learn commit structure, commit messages, amend, signed commits, and best practices used by DevOps teams.
External Resources & Further Reading
For more information about git staging and version control workflows, check out these resources:
- Official Git Documentation – git-add(1) Manual: https://git-scm.com/docs/git-add
- Pro Git Book – Chapter 2.2 (Staging): https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository
- GitHub Help – Staging changes: https://docs.github.com/en/repositories/working-with-files/managing-files/adding-a-file-to-a-repository