Staging and Committing Changes
Git's staging area gives you precise control over what goes into each commit. This lesson covers the workflow from editing files to creating permanent history.
The Three Areas of Git
Understanding Git's architecture is key to mastering it:
1┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐2│ Working Tree │───▶│ Staging Area │───▶│ Repository │3│ (your files) │ │ (index) │ │ (.git) │4└─────────────────┘ └─────────────────┘ └─────────────────┘5 edit git add git commit
- Working Tree: Your actual files on disk
- Staging Area: Files marked for the next commit
- Repository: Permanent commit history
Checking Status
The git status command shows the current state:
bash1$ git status2On branch main3Your branch is up to date with 'origin/main'.45Changes not staged for commit:6 (use "git add <file>..." to update what will be committed)7 (use "git restore <file>..." to discard changes in working directory)8 modified: README.md910Untracked files:11 (use "git add <file>..." to include in what will be committed)12 new-file.txt1314no changes added to commit (use "git add" and/or "git commit -a")
Short Status
For a compact view, use -sb:
bash1$ git status -sb2## main...origin/main3 M README.md4?? new-file.txt
Status codes:
M= ModifiedA= Added (staged)D= Deleted??= UntrackedR= Renamed
Staging Files
Stage Specific Files
bash1$ git add README.md2$ git add new-file.txt
Stage All Changes
bash1$ git add . # Everything in current directory2$ git add -A # Everything in entire repository
Stage Interactively
Review and select specific hunks:
bash1$ git add -p README.md
This shows each change and asks:
y= stage this hunkn= skip this hunks= split into smaller hunksq= quit
Reviewing Changes
Unstaged Changes
See what you've modified but not staged:
bash1$ git diff2diff --git a/README.md b/README.md3index f0e1d2c..a5b6c7d 1006444--- a/README.md5+++ b/README.md6@@ -1 +1,3 @@7 # Demo Repo8+9+This is a demo repository for learning Git.
Staged Changes
See what will be committed:
bash1$ git diff --cached
Or equivalently:
bash1$ git diff --staged
Committing
Basic Commit
bash1$ git commit -m "docs: add description to README"2[main 2b3c4d5] docs: add description to README3 1 file changed, 2 insertions(+)
Commit with Editor
For longer messages, omit -m:
bash1$ git commit
This opens your editor. Write:
- First line: Short summary (50 chars)
- Blank line
- Body: Detailed explanation
Conventional Commit Format
Use prefixes to categorize commits:
| Prefix | Use For |
|---|---|
feat: | New features |
fix: | Bug fixes |
docs: | Documentation |
style: | Formatting (no code change) |
refactor: | Code restructuring |
test: | Adding tests |
chore: | Maintenance tasks |
Example:
1feat: add user authentication23- Implement JWT token generation4- Add login/logout endpoints5- Create user session middleware
Inspecting History
Basic Log
bash1$ git log2commit 2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c3Author: Ada Lovelace <ada@example.com>4Date: Sun Nov 30 10:15:00 2025 +000056 docs: add description to README
Compact Log
bash1$ git log --oneline22b3c4d5 docs: add description to README31a2b3c4 docs: add initial README
Graph View
See branch structure:
bash1$ git log --oneline --graph --decorate2* 2b3c4d5 (HEAD -> main, origin/main) docs: add description to README3* 1a2b3c4 docs: add initial README
Filter by Author or Date
bash1$ git log --author="Ada"2$ git log --since="2 weeks ago"3$ git log --grep="fix"
Best Practices
Commit Often, Commit Small
Each commit should be a single logical change. This makes:
- History easier to understand
- Reverting specific changes possible
- Code review more manageable
Write Good Messages
Bad:
fixed stuff
Good:
1fix: prevent crash when user input is empty23The application crashed when users submitted forms without4filling required fields. Added null checks to validation.
Review Before Committing
Always check git diff --cached before committing to ensure you're including exactly what you intend.
Key Takeaway
The staging area lets you craft precise commits. Use git status to understand state, git add -p for selective staging, git diff --cached to review, and write meaningful commit messages.