Lesson

Git Staging

10 min reading
Free Course

Git Staging: Controlling What Enters Your Next Commit

The Git staging area (also called the index) is a preparation zone between your working directory and your permanent commit history. It lets you review and group related edits into clean, focused commits instead of saving every modified file at once.

Git's Three-Tree Architecture

Git manages your project across three distinct areas: your working files on disk, the staging index, and the committed repository history.

flowchart LR
    W["Working Directory<br/>(Local Files)"] -->|"git add"| S["Staging Area<br/>(Index File)"]
    S -->|"git commit"| R["Commit History<br/>(Git Object Database)"]

Staging Specific Files vs Everything

Stage individual files when you want separate commits for unrelated tasks, such as fixing layout styling separately from documentation updates:

git add index.html styles.css

When every change in your workspace belongs to the same task, stage all new, modified, and deleted files in the repository with -A (or --all):

git add -A

If you edited two unrelated things inside the same file, use interactive patch staging to stage only specific hunks:

git add -p styles.css

Reviewing and Unstaging Changes

Always verify what is staged versus what remains unstaged before committing:

git status
git diff --staged

git diff --staged prints the exact line-by-line changes sitting in the staging area. If you staged a file like .env or a debug edit by mistake, remove it from the index without losing your local edits:

git restore --staged styles.css

Common Staging Mistakes

Running git add . blindly from a subfolder only stages files in that directory and its children on older Git habits, or stages temporary files you forgot to ignore. Always run git status right after staging to confirm the exact file list.

Another mistake is editing a staged file again and committing immediately. Git commits the snapshot from when you ran git add, so you must run git add again to include your newer edits.

Frequently Asked Questions

What is the difference between git add . and git add -A?

In modern Git (2.x+), running git add . from the repository root behaves identically to git add -A by staging new, modified, and deleted files. Running git add . inside a subdirectory only stages changes within that subdirectory, whereas git add -A covers the entire repository.

Does git restore --staged delete my work?

No. git restore --staged <file> only removes the snapshot from the staging area. Your modifications remain safely in your working directory.

Why does Git have a staging area at all?

The staging area lets you craft clean, atomic commits. Even if you edited five files while debugging, you can stage and commit two files for a bug fix and stage the remaining three for a separate refactor.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum