git add Command Guide
Stage changes for the next commit
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git add <file>Stage specific filegit add .Stage all changesgit add -AStage all (including deletes)Selective
git add -pInteractive staginggit add -iInteractive modegit add *.jsStage by patternAdvanced
git add -uStage modified/deleted onlygit add -nDry run (show what would be added)git add -fForce add ignored filesDownloadable Image Preview
Basic Usage
The git add command adds changes to the staging area (index). This is the first step in the Git workflow before committing changes.
# Stage a specific file
git add filename.js
# Stage multiple files
git add file1.js file2.js file3.js
# Stage all changes in current directory and subdirectories
git add .
# Stage all changes in the entire repository
git add -A
git add --allCommon Options
| Option | Description |
|---|---|
| -A, --all | Stage all changes (new, modified, deleted) |
| -p, --patch | Interactively choose hunks to stage |
| -u, --update | Stage modified and deleted files only |
| -n, --dry-run | Show what would be added without adding |
| -f, --force | Add ignored files |
| -i, --interactive | Interactive mode |
Interactive Staging
Use git add -p to stage changes interactively, selecting which parts of files to include:
$ git add -p
diff --git a/src/app.js b/src/app.js
@@ -1,5 +1,6 @@
const app = {
name: 'MyApp',
+ version: '2.0',
init() {
console.log('Starting...');
}
Stage this hunk [y,n,q,a,d,s,e,?]?Interactive Commands
y- Stage this hunkn- Do not stage this hunkq- Quit; do not stage this or remaining hunksa- Stage this and all remaining hunks in files- Split this hunk into smaller hunkse- Manually edit this hunk
git add -p is excellent for creating focused, atomic commits by selecting only related changes.Staging Patterns
# Stage all JavaScript files
git add *.js
# Stage all files in a directory
git add src/
# Stage all test files recursively
git add **/*.test.js
# Stage all new and modified files (not deleted)
git add .
# Stage all modified and deleted files (not new)
git add -uPractical Examples
# Typical workflow: stage and commit
git add .
git commit -m "Add new feature"
# Stage specific changes interactively
git add -p src/app.js
# Preview what will be staged
git add -n .
# Stage all changes except untracked files
git add -u
# Force add a file that's in .gitignore
git add -f build/config.json
# Undo staging (unstage all files)
git restore --staged .Frequently Asked Questions
What is the difference between "git add ." and "git add -A"?
In Git 2.x, they are essentially the same when run from the root directory. "git add -A" stages all changes (new, modified, deleted) from the entire repository, while "git add ." stages changes from the current directory and below.
How do I unstage a file after git add?
Use "git restore --staged <file>" or "git reset HEAD <file>" to unstage a file while keeping the changes in your working directory.
Can I add only part of a file's changes?
Yes! Use "git add -p <file>" to interactively select which hunks (sections) of changes to stage. You can stage some changes while leaving others unstaged.
How do I add files that are in .gitignore?
Use "git add -f <file>" to force add ignored files. However, consider if you really need to track these files.
What does "git add -u" do?
It stages all modifications and deletions of tracked files, but does NOT add new untracked files. Useful when you want to commit changes to existing files only.
Summary
git add is essential for staging changes before committing. Master interactive staging for cleaner, more focused commits.
Quick Reference
git add <file>- Stage specific filegit add .- Stage all changesgit add -p- Interactive staginggit add -u- Stage modified/deleted onlygit add -A- Stage everything
Official Documentation
For authoritative information, refer to the official documentation: