git add
Staging changes with git add — how to prepare files for the next commit.
What you'll learn
- How to stage individual files, multiple files, or everything
- The difference between git add . and git add -A
- How to stage parts of a file with git add -p
- What happens when you stage a file that is already staged
Concept
What git add Does
git add moves changes from your working directory into the staging area. It tells Git: "I want to include these changes in my next commit." Nothing is saved to history yet — that happens with git commit.
git add README.md
Staging Options
# Stage a single file
git add README.md
# Stage multiple specific files
git add index.html style.css script.js
# Stage all changes in the current directory and below
git add .
# Stage all changes in the entire repository (including deletions)
git add -A
# or equivalently:
git add --all
# Stage all changes in the current directory (including deletions here)
git add -u
git add . vs git add -A
This is a subtle but important distinction:
git add .— stages new and modified files in the current directory and its subdirectories. It does NOT stage deletions in older Git versions (pre-2.0). In modern Git (2.0+), it stages everything in the current directory tree.git add -A— stages everything in the entire repository, including deletions, regardless of which directory you are in.
Best practice: when starting out, use git add -A or git add . from the repository root. As you advance, stage files individually to keep commits focused.
Staging Parts of a File
Sometimes you have multiple unrelated changes in the same file and want to commit them separately. git add -p (patch mode) lets you stage changes hunk by hunk:
git add -p script.js
Git will show you each change and ask: y (yes, stage this hunk), n (no), s (split into smaller hunks), q (quit). This is powerful for creating clean, focused commits.
Re-staging a Modified File
If you stage a file, then modify it again, the new changes are not staged. The staging area holds a snapshot from when you ran git add. You need to git add the file again to stage the latest changes:
git add README.md # stage version 1
echo "more text" >> README.md # now version 2
git status # shows README as both staged AND modified
git add README.md # re-stage version 2
What git add Does NOT Do
- It does not save anything to history — that is
git commit. - It does not push to a remote — that is
git push. - It does not permanently lock the changes — you can unstage with
git reset.
Example
# Create some files to work with
echo "# My Project" > README.md
echo "body { margin: 0; }" > style.css
echo "console.log('hi');" > script.js
# Stage a single file
git add README.md
git status # README.md is staged, the others are untracked
# Stage the rest
git add style.css script.js
git status # all three are now staged
# Modify a file after staging
echo "## Installation" >> README.md
git status # README.md is now both staged AND modified
# Re-stage the updated file
git add README.md
git status # README.md is fully staged again
# Stage everything at once (common workflow)
git add .
git status
# Interactive staging — stage parts of a file
git add -p script.js
Shows staging individual files, staging multiple files, the re-staging requirement after modification, staging everything, and interactive patch mode. Use git status after each command to see what is staged.
Try it
- Code Diff
Compare two versions — Git shows diffs when you stage and commit.
- .gitignore Generator
Keep files out of staging with a proper .gitignore.
Common mistakes
The mistake
Staging a file, editing it, and forgetting to re-stage before committing
The fix
git add takes a snapshot at the moment you run it. If you edit the file afterward, the new changes are not staged. Run git add again before committing, or check git status to see if a file is both staged and modified.
The mistake
Using git add . and accidentally staging files that should be ignored
The fix
Create a .gitignore file first to exclude build artifacts, node_modules, .env files, etc. Then git add . will skip them automatically.
Related Resources
Related Cheatsheets
- Git Cheatsheet
git add options and staging patterns.
Practice git add
2 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.