git add
Add file contents to the index (staging area)
The 'git add' command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. The index, also known as the staging area, is what you use to prepare the contents of the next commit. When you run 'git commit' without any other arguments, it will only commit staged changes. This command can be performed multiple times before a commit to add incremental changes.
Synopsis
git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [--all | -A] [--update | -u] [--] [<pathspec>...]Options
Choosing the Right Option
Staging Commands
Understanding which files get staged with different commands
| Command | New Files | Modified Files | Deleted Files | Scope |
|---|---|---|---|---|
git add <file> | ✓ | ✓ | ✓ | Specific file only |
git add . | ✓ | ✓ | ✓ | Current dir & below |
git add -A | ✓ | ✓ | ✓ | Entire repository |
git add -u | ✗ | ✓ | ✓ | Tracked files only |
git add --no-all | ✓ | ✓ | ✗ | Ignores deletions |
Interactive Options
Options for fine-grained control over what gets staged
| Command | Mode | Granularity | Use Case |
|---|---|---|---|
git add -p | Patch | Hunk-by-hunk | Stage specific changes within a file |
git add -e | Editor | Line-by-line | Manually edit diff before staging |
git add -i | Menu | File-by-file | Full interactive staging menu |
Preview & Output Options
Options that show information without modifying the staging area
| Command | Action | Modifies Index | Purpose |
|---|---|---|---|
git add -n | Dry-run | ✗ | Preview what would be staged |
git add -v | Verbose | ✓ | Show files as they're staged |
git add --refresh | Refresh | ✗ | Update stat info only |
Special File Handling
Options for handling files that need special treatment
| Command | Handles | Behavior | Typical Use |
|---|---|---|---|
git add -f | Ignored files | Force past .gitignore | Add a file that's normally ignored |
git add -N | New files | Intent to add (empty) | Track file for git diff without staging |
git add --chmod=+x | Executables | Set file mode | Make scripts executable |
git add --sparse | Sparse files | Outside cone | Stage files outside sparse checkout |
Advanced Options
Expert-level options for specific scenarios
| Command | Purpose | When to Use |
|---|---|---|
git add --renormalize | Re-apply clean filters | After changing .gitattributes line endings |
git add --pathspec-from-file | Read paths from file | Staging many files from a list |
git add --ignore-errors | Continue on errors | Stage what you can, skip failures |
git add --no-warn-embedded-repo | Suppress submodule warning | Intentionally adding nested repos |
Workflow Examples
git statusReview all changes first
git add -AStage all new, modified, and deleted files
git commit -m "Your message"Commit all staged changes
git add -pInteractively select only the bug fix changes (press 'y' for bug fix hunks, 'n' for feature hunks)
git commit -m "Fix login validation bug"Commit just the bug fix
git add -ANow stage the remaining feature changes
git commit -m "Add dark mode toggle"Commit the feature separately
git statusSee what's modified vs untracked
git add -uStage only modifications to tracked files
git statusVerify new files are still untracked
git commit -m "Update existing files"Commit without the new files
git add -n .Dry run shows what would be added
git diffReview the actual changes in detail
git add .If satisfied, stage for real
git add dist/bundle.jsThis fails - file is ignored
git add -f dist/bundle.jsForce add overrides .gitignore
git commit -m "Add build for deployment"Commit the normally-ignored file
Interactive Mode Keys
Keyboard shortcuts for git add's interactive modes. These work in patch mode (-p) and interactive mode (-i).
Patch Mode
-pInteractive Mode
-iTroubleshooting
File is not being added (ignored)
Cause: The file matches a pattern in .gitignore
Check .gitignore for matching patterns. Use -f to force add if intentional.
git add -f <file>Permission denied error
Cause: File is locked by another process or you lack read permissions
Close any applications using the file, or check file permissions. Use --ignore-errors to continue with other files.
git add --ignore-errors .Nothing happens when running git add
Cause: Files may already be staged, or there are no changes to add
Run git status to see current state. Files already staged won't be re-added.
git statusNew files not showing in git diff
Cause: Untracked files don't appear in git diff by default
Use git add -N to mark intent-to-add, then git diff will show the content.
git add -N <file> && git diffWarning about embedded git repository
Cause: You're adding a folder that contains its own .git directory (a nested repo)
Either remove the nested .git folder, use git submodules, or suppress the warning.
git add --no-warn-embedded-repo <folder>Line endings keep showing as changes
Cause: Inconsistent line endings (CRLF vs LF) between your system and the repo
Configure .gitattributes with 'text=auto' and renormalize all files.
git add --renormalize .Scripts (e.g. git diff-files) list files as modified but content is unchanged
Cause: File timestamps changed, so the stat info cached in the index is stale
Refresh the index to update stat info without staging. (git status refreshes automatically.)
git add --refresh .Tips & Pitfalls
Best Practices
- ✓Review changes before staging: Use 'git diff' to see what you're about to stage, or 'git add -p' to review interactively.
- ✓Stage related changes together: Group logically related changes in a single commit. Don't mix bug fixes with new features.
- ✓Use 'git add -p' for large changes: When you've made multiple unrelated changes to a file, use patch mode to stage them separately.
- ✓Check git status frequently: Always run 'git status' before and after staging to verify what will be committed.
- ✓Be cautious with 'git add .': This stages everything in the current directory. Consider using specific filenames or 'git add -p' for more control.
- ✓Set up .gitignore first: Before your first 'git add', ensure .gitignore is configured to exclude build artifacts, dependencies, and sensitive files.
- ✓Use dry-run for verification: When unsure, use 'git add -n .' to preview what would be staged without actually staging.
- ✓Stage deletions explicitly: Remember that deleting a file locally doesn't stage the deletion. Use 'git add -u' or 'git add -A' to include deletions.
Common Mistakes
- ✗Using 'git add .' vs 'git add -A': Both stage new, modified and deleted files, but '.' only covers the current directory down, while '-A' covers the entire working tree.
- ✗Forgetting that 'git add' does not commit changes. The files are only staged and require 'git commit' to be recorded.
- ✗Adding files that should be ignored. Always check your .gitignore before running 'git add -A' or 'git add .'
- ✗Not reviewing changes before staging. Use 'git add -p' for a more careful review of what you're staging.
- ✗Confusing 'git add -u' with 'git add -A': The '-u' flag only stages modifications and deletions of tracked files, not new untracked files.
FAQ
Since Git 2.0 both stage new, modified and deleted files. The difference is scope: 'git add .' covers only the current directory and subdirectories, while 'git add -A' covers the entire repository, regardless of your current directory.
The file is likely listed in .gitignore. Check with 'git check-ignore <file>'. If you need to add it anyway, use 'git add -f <file>' to force.
Yes! Use 'git restore --staged <file>' (or 'git reset HEAD <file>' in older Git versions) to unstage a file without losing your changes.
Use 'git add -p <file>' to enter interactive patch mode. Git will show you each change (hunk) and let you choose which ones to stage with y/n.
This usually happens due to line ending changes (CRLF/LF) or file permission changes. Check 'git diff' to see what changed. Configure .gitattributes to handle line endings.
Add the files you want to exclude to .gitignore first, then use 'git add .'. Alternatively, add files individually or use patterns like 'git add *.js'.
It tells Git you intend to add the file later. The file becomes tracked (appears in git diff) but the content isn't staged yet. Useful for reviewing new files before committing.
No, Git only tracks files, not directories. The common workaround is to add a .gitkeep or .gitignore file inside the empty directory.