Adding files is how you move changes from the working directory to the staging area. This two-step process helps you control exactly what gets committed. You can stage all files at once or be selective about which specific files to include.
Staging Changes
git add . stages all modified files, git add filename works for specific files. The staging area is your "pending changes" zone โ this gives you a chance to review and organize before making a commit. Any file in the staging area will become part of your next commit.
$ git status
$ git add .
$ git add README.md
$ git status
Why the Staging Area Exists
You might wonder why Git requires you to stage changes before committing. The staging area gives you three important advantages:
- Selective commits: You can modify multiple files but only commit some of them, keeping unrelated changes separate.
- Precise control: Stage specific portions of a file using
git add -p(patch mode), which lets you choose which changes to include. - Review before saving: Running
git statusshows you exactly what will be committed, preventing accidental saves.
# Stage only specific lines (interactive mode)
$ git add -p
# Git will show each change and ask:
# Stage this hunk [y,n,q,a,d,e,?]? y
Unstaging Files
If you accidentally staged a file you didn't mean to, you can undo it without losing any changes:
# Unstage a specific file
$ git reset HEAD filename
# Unstage everything
$ git reset HEAD
# Unstage a specific hunk
$ git reset -p
The git reset command moves changes from the staging area back to the working directory. Your actual file changes are preserved โ only the staging state is reversed.
The .gitignore File
Not every file should be tracked by Git. Configuration files with passwords, compiled binaries, and temporary files should be excluded. Create a .gitignore file in your project root:
# .gitignore example
node_modules/
.env
*.log
.DS_Store
dist/
__pycache__/
Git will ignore any file or directory matching these patterns. This keeps your repository clean and prevents sensitive information from being accidentally committed.