git add
Stage file changes for the next commit
Syntax
git add [<options>] [--] <pathspec>...
Parameters
| Parameters | Description | Examples | Level |
|---|---|---|---|
. |
Add all changes in the current directory | git add . |
Common |
-A --all |
Add all changes (including deletions) | git add -A |
Common |
-p --patch |
Interactively select code hunks to stage | git add -p |
Advanced |
-u --update |
Only add changes to tracked files | git add -u |
Common |
-n --dry-run |
Simulate adding without actually executing | git add -n . |
Advanced |
Examples
Add a single file
git add README.md
Add an entire directory
git add src/
Recursively add all changes under the src directory
Add all changes
git add -A
Including new files, modifications, and deletions
Interactive staging
git add -p
Select changes hunk by hunk, ideal for splitting commits
Common Errors
fatal: pathspec 'xxx' did not match any files
Check if the file path is correct and the file exists
Tips
- Use `git add -p` to split changes in a single file into multiple commits
- Use `git status` to confirm staging area status before committing