git
version control
multiple files
command line
git add

How to add multiple files to Git at the same time

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Staging multiple files efficiently is a core Git workflow skill, especially in large repositories with frequent edits. Git offers several ways to stage groups of files based on path, type, or interactive hunks. Choosing the right git add pattern keeps commits focused and review-friendly.

Add Specific Files in One Command

Provide multiple paths explicitly:

bash
git add src/app.ts src/utils/time.ts README.md

This is precise and safe for small file sets.

Add All Changes in Current Directory Tree

To stage everything under current path:

bash
git add .

This includes new files, modifications, and deletions under the directory where command is executed.

Add Changes Across Entire Repository

From repo root, common patterns include:

bash
git add -A

or:

bash
git add --all

These stage all tracked and untracked changes across the repository.

Stage by File Pattern

Use shell globs or pathspecs to stage classes of files.

bash
git add "src/**/*.ts"
bash
git add "docs/*.md"

Quote patterns so shell expansion behavior stays predictable across environments.

Stage Only Modified and Deleted Tracked Files

If you do not want new untracked files staged:

bash
git add -u

This updates tracked files only and is useful for clean maintenance commits.

Interactive Staging for Partial Changes

When one file has multiple logical edits, stage chunks selectively.

bash
git add -p

This opens hunk-by-hunk prompts so you can split unrelated edits into separate commits.

Useful Verification Commands

Before committing, always inspect staged state.

bash
git status

See staged diff only:

bash
git diff --cached

These checks prevent accidental inclusion of unrelated files.

Practical Multi-File Workflow

Example for feature work:

bash
1# 1. Edit files
2# 2. Stage feature-related files
3git add src/auth/login.ts src/auth/session.ts tests/auth/login.test.ts
4
5# 3. Verify staged content
6git diff --cached
7
8# 4. Commit
9git commit -m "Implement session refresh flow"

Repeat for docs or refactor files in separate commits.

Avoiding Common Over-Staging Problems

Large repositories make git add . risky when many local changes exist. Safer alternatives:

  • Stage explicit paths.
  • Use git add -p for granular control.
  • Use git restore --staged <path> to unstage mistakes.

Example unstaging:

bash
git restore --staged docs/draft.md

This keeps commit scope intentional.

Working with .gitignore

Ignored files are skipped by default. If needed, force-add one ignored file:

bash
git add -f .env.example

Use force-add sparingly and only for files meant to be versioned.

Stage from Generated File Lists

In automation, you can stage many files from a list file to keep shell commands deterministic.

bash
1cat changed-files.txt
2# src/a.ts
3# src/b.ts
4# docs/guide.md
5
6xargs -I{} git add \"{}\" < changed-files.txt

If file names may contain spaces, generate null-delimited lists and use xargs -0 for safety.

Performance Tips in Large Repositories

In very large monorepos:

  • Run git status before staging to understand scope.
  • Stage by directory to keep command latency lower.
  • Prefer smaller logical commits rather than one giant staged snapshot.

This improves review quality and reduces merge conflict complexity.

Common Pitfalls

  • Using git add . from wrong directory level. Fix by checking current path and git status before staging.
  • Staging unrelated edits in one commit. Fix by using explicit file lists or git add -p.
  • Forgetting deleted files when staging. Fix by using git add -A or git add -u as needed.
  • Assuming ignored files are staged automatically. Fix by using -f only when intentional.
  • Committing without reviewing staged diff. Fix by checking git diff --cached every time.

Summary

  • Git can stage multiple files via explicit paths, globs, or full-tree commands.
  • git add -A stages everything, while git add -u stages tracked-file updates only.
  • Interactive staging helps create clean, focused commits.
  • Always verify staged changes before commit.
  • Intentional staging produces better history and easier code reviews.
  • Smaller commits speed review cycles.

Course illustration
Course illustration

All Rights Reserved.