NeuroAgent

Git Add --all vs .: Complete Guide

Learn the key differences between git add --all and git add . commands. Understand how they handle deleted files, directory scope, and when to use each in your Git workflow.

Question

What is the difference between git add [--all | -A] and git add . commands in Git version control?

NeuroAgent

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

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 shorthand git 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 --all stages deletions, meaning files that have been deleted from the working directory will be removed from the repository when committed
  • git add . ignores deleted files, so you’ll need to use git rm to 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

bash
# 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

bash
# 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:

bash
# 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:

bash
# 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 .:

bash
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:

bash
# 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.


Sources

  1. Official Git Documentation - git-add
  2. Atlassian Git Tutorial - Staging Files
  3. Git Pro Book - Staging Area
  4. Stack Overflow - Difference between git add -A and git add .
  5. GitHub Docs - Adding Files to Your Repository