Git helps you track changes in your project, collaborate with others, and maintain version history. But before you can commit changes to your Git repository, you need to add files to the staging area.
In this post, you’ll learn how to add files to Git, from individual files to entire folders, and best practices for managing what gets tracked.
📦 What Does “Adding Files to Git” Mean?
When you run a git add
command, you’re telling Git:
“I want to include these changes in the next commit.”
This includes:
- New files
- Modified files
- Deleted files
These changes are added to the staging area, also known as the index. Once staged, they can be committed with git commit
.
🚀 Basic Syntax
git add <file-name>
Example:
git add index.html
This stages the file index.html
for the next commit.
📁 Add All Files
To add all modified and new files in the current directory and subdirectories:
git add .
or
git add -A
✅
git add .
stages only tracked and newly created files in the current directory.
✅git add -A
stages all changes—including file deletions.
📄 Add Specific Files or Folders
Add multiple files:
git add file1.py file2.css
Add an entire folder:
git add src/
This adds all changes in the src
folder recursively.
❌ Exclude Files Using .gitignore
If there are files you never want to track (e.g., logs, environment files, build outputs), use a .gitignore
file.
Example .gitignore
:
node_modules/
*.log
.env
Git will skip these when running git add .
.
✅ Check What’s Staged
Before committing, you can check which files have been staged:
git status
This shows:
- Untracked files
- Modified files
- Staged files
🧠 Best Practices
- Review with
git status
before committing - Use
.gitignore
to avoid adding unwanted files - Don’t blindly use
git add .
in large projects—be intentional - Commit in logical chunks (group related changes)
🔁 Common Commands Summary
Task | Command |
---|---|
Add single file | git add filename |
Add multiple files | git add file1 file2 |
Add all files in directory | git add . |
Add all changes (inc. deletes) | git add -A |
Check staged files | git status |
📘 Final Thoughts
Adding files is the first step in telling Git what to track in your project’s version history. Whether you’re committing a single bug fix or preparing a full feature branch, understanding how to stage changes correctly is key to maintaining a clean, reliable codebase.