What is the difference between git add [--all | -A] and git add . commands in Git version control?
The key difference between git add [--all | -A] and git add . is that git add --all (or -A) adds all changes including untracked files, deleted files, and modified files regardless of the current directory, while git add . only adds changes in the current directory and its subdirectories, excluding files that have been deleted from the working directory.
Contents
- Understanding Git Add Commands
- Detailed Comparison
- Practical Examples
- When to Use Each Command
- Common Pitfalls
Understanding Git Add Commands
Git’s add command is fundamental to the version control workflow, responsible for staging changes before committing them to the repository. While both git add --all and git add . appear to add files, they operate differently based on their scope and behavior.
Basic Definitions
-
git add --all(or its shorthandgit add -A): Adds all changes across the entire repository, including:- Modified files
- New untracked files
- Deleted files
- Works from any directory
-
git add .: Adds changes only in the current directory and subdirectories, including:- Modified files
- New untracked files
- Excludes deleted files from the working directory
Detailed Comparison
Scope Differences
| Feature | git add --all / -A |
git add . |
|---|---|---|
| Directory scope | Entire repository | Current directory and subdirectories |
| Deleted files | ✅ Includes deleted files | ❌ Excludes deleted files |
| Untracked files | ✅ Includes all untracked files | ✅ Includes untracked files in scope |
| File modifications | ✅ Includes all modifications | ✅ Includes modifications in scope |
| Current directory dependency | ❌ Works from any directory | ✅ Must be run from target directory |
Behavioral Differences
The most critical difference lies in how these commands handle deleted files:
git add --allstages deletions, meaning files that have been deleted from the working directory will be removed from the repository when committedgit add .ignores deleted files, so you’ll need to usegit rmto stage deletions when using this command
Practical Examples
Scenario Setup
Let’s consider a repository structure:
project/
├── file1.txt (modified)
├── file2.txt (deleted)
├── subdir/
│ ├── file3.txt (modified)
│ └── file4.txt (new)
Running Commands from Project Root
# From project root directory
git add --all # Stages all changes including file2.txt deletion
git add . # Stages file1.txt, subdir/file3.txt, subdir/file4.txt
# Does NOT stage file2.txt deletion
Running Commands from Subdirectory
# From project/subdir directory
git add --all # Still stages all changes including file2.txt deletion
git add . # Only stages subdir/file3.txt and subdir/file4.txt
# Does NOT stage file1.txt or file2.txt deletion
Visual Representation
Before staging:
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file1.txt
modified: subdir/file3.txt
new file: subdir/file4.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file2.txt
deleted: file2.txt
After git add --all:
$ git add --all
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file1.txt
deleted: file2.txt
modified: subdir/file3.txt
new file: subdir/file4.txt
After git add . (from root):
$ git add .
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file1.txt
modified: subdir/file3.txt
new file: subdir/file4.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: file2.txt
When to Use Each Command
Use git add --all when:
- You want to stage all changes in the repository
- You’re working on multiple directories and want to commit everything
- You’ve deleted files and want them removed from the repository
- You’re cleaning up and want to commit all outstanding changes
- You prefer a “commit everything” approach
Use git add . when:
- You’re working on a specific directory and only want to stage changes there
- You want more granular control over what gets committed
- You’re not ready to commit deletions yet
- You’re working incrementally and want to commit changes piece by piece
- You want to avoid accidentally staging unintended changes
Alternative Approaches
For more precise control, consider these alternatives:
# Stage only modified and new files (not deletions)
git add -u # Update - stages modified and deleted files, but not new files
git add -A # Stage everything (modified, new, deleted)
git add . # Stage everything in current directory (except deletions)
Common Pitfalls
Unexpected Behavior with Deleted Files
The most common issue is forgetting that git add . doesn’t stage deletions:
# This doesn't work as expected for deletions
git add .
git commit -m "Cleanup"
# Result: Deleted files remain in the repository!
Solution: Use git rm for deletions when using git add .:
git rm file2.txt
git add .
git commit -m "Cleanup"
Directory-Specific Limitations
Running git add . from the wrong directory can cause you to miss important changes:
# From wrong directory
cd subdir/
git add . # Only stages changes in subdir, misses root directory changes
Solution: Either run from the root directory or use git add --all regardless of current location.
Performance Considerations
While both commands are generally fast, git add --all can be slower in large repositories because it scans the entire working tree, whereas git add . only scans the current directory.
Conclusion
The difference between git add --all and git add . boils down to scope and deletion handling. git add --all stages all changes across the entire repository including deletions, while git add . only stages changes in the current directory and subdirectories, excluding deletions. Choose git add --all for comprehensive commits and git add . for more targeted, directory-specific staging. Understanding this distinction helps prevent common Git workflow mistakes and ensures your commits contain exactly what you intend.