Git Notes
Master the git add command for staging changes, understanding the staging area, staging specific files, patterns, and preparing commits for version control.
git add moves changes from your working directory to the staging area, indicating which changes to include in the next commit. Understanding staging is key to Git mastery.
The Staging Area Concept
| Working | add | Staging | commit | Repository |
|---|---|---|---|---|
| Directory | ──────→ | Area | ─────→ | (committed) |
| (your files) | (what's new) | (permanent) |
Basic Usage
# Stage single file
git add README.md
# Stage specific files
git add index.html style.css
# Stage all changes
git add .
# Stage all changes (verbose)
git add -AViewing Staged Changes
# See what's staged (not yet committed)
git diff --staged
# Or equivalently
git diff --cached
# Output shows: + for additions, - for deletionsStage Selectively
# Made 5 changes but only want to stage 2
git status
# Modified: file1.js
# Modified: file2.js
# Modified: file3.js
# Modified: file4.js
# Modified: file5.js
# Stage only related changes
git add file1.js file2.js
git commit -m "Feature: Add user auth"
# Stage others later
git add file3.js
git commit -m "Fix: Update styling"Using Patterns
# All Python files
git add *.py
# All files in folder
git add src/
# All Java and Python
git add "*.java" "*.py"
# Everything except test files
git add .
git reset "*_test.js"Interactive Staging
# Review changes interactively
git add -i
# Options:
# s - staged area
# a - unstaged area
# r - revert changes
# p - patch (stage parts of files)Patch Mode (Stage Parts)
Undo Staging
# Unstage file (keep changes)
git reset README.md
# Or explicitly
git restore --staged README.md
# Verify
git status
# README.md shows as "Modified" not "Changes to be committed"Check What's Staged
git status
# Output:
# Changes to be committed:
# new file: README.md
# modified: index.html
# deleted: old-file.js
# Changes not staged for commit:
# modified: style.cssStage Everything
# Add all modified files (not new ones)
git add -u
# Add all changes (modified and new)
git add .
git add -A
git add --allCommon Patterns
Add all Python files
git add "*.py"Add entire directory
git add src/Add new files only
git add --no-ignore -uBest Practices
Don't do this:
git add .
git commit -m "Fixed stuff"
# Commits unrelated changes together!Do this instead:
# Stage related changes
git add auth.js
git commit -m "Add login functionality"
# Stage different feature
git add styles.css
git commit -m "Update theme colors"
# Logical, focused commitsInterview Q&A
Q1: What is the staging area and why does it exist?
A: Staging area is intermediate step between working directory and repository. Allows selective inclusion of changes. You might have 5 files changed but want to commit only 2 related files. Staging enables logical, focused commits instead of committing everything at once. Better for code review and history clarity.
Q2: Difference between git add . and git add -A?
A: Both add all changes but git add . adds changes in current directory and subdirectories. git add -A adds changes everywhere in repository. In practice they're usually equivalent, but -A is more explicit and safer for large repositories.
Q3: How do you stage only part of a file?
A: Use git add -p (patch mode) which reviews each change interactively. For each hunk, Git asks "Stage this chunk?" You can say yes, no, or split to stage specific lines. Perfect for separating unrelated changes made in same file.
Q4: How do you undo staging without losing changes?
A: Use git reset filename to unstage. Changes remain in working directory, just removed from staging area. Verify with git status showing file as "Modified" not "Changes to be committed". Compare to git reset --hard which loses changes.
Q5: Why is selective staging better than git add .?
A: Selective staging enables atomic commits - focused on single feature/fix. Makes history clear: each commit shows one logical change. Easier for code review: reviewer sees related changes together. Better for debugging: git bisect can find exact commit causing issue. Easier to revert specific feature without affecting others.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Add - Stage Changes.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, basics, add, git add - stage changes
Related Git & GitHub Topics