git/add

Add Files

Stage file changes to the Git index (staging area) in preparation for committing

git
add
files
stage
staging area
index

Command

git add

Explanation

The git add command stages changes, adding modified, new, or deleted files to the Git staging area (also called the index). This is an essential step in the Git workflow between making changes and committing them. Staging allows you to selectively choose which changes to include in your next commit, enabling you to make logical, atomic commits rather than committing everything at once. You can stage individual files, multiple files, entire directories, or all changes at once. The interactive mode (-p flag) is particularly powerful, allowing you to review and stage specific parts of files line by line, which is invaluable for creating focused commits. Once files are staged, they appear in "Changes to be committed" when you run git status. The staging area acts as a buffer between your working directory and repository history, giving you control over what gets committed. This command is used multiple times in typical development workflows and is fundamental to understanding Git's three-stage architecture: working directory, staging area, and repository. Proper use of git add helps maintain clean commit history and makes code reviews more manageable.

Common Use Cases

  • Staging specific files for a focused commit
  • Preparing changes for commit after reviewing modifications
  • Selectively staging parts of files using interactive mode
  • Adding new files to version control
  • Staging all changes for a complete commit

Best Practices

  • Review changes with git status and git diff before staging
  • Use git add -p for granular control over what to commit
  • Stage related changes together for logical commits
  • Check .gitignore before adding files to avoid tracking unwanted items
  • Use git add . carefully - prefer specifying files or directories explicitly
  • Review staged changes with git diff --staged before committing

Common Mistakes to Avoid

  • Using git add . when you only want to stage specific files
  • Staging unwanted files like build artifacts or temporary files
  • Confusing git add with git commit (add stages, commit saves)
  • Not using -p flag when you want to commit only part of file changes
  • Adding files that should be in .gitignore

Troubleshooting

Problem: File not staging after git add

Solution: Check if file is in .gitignore. Remove from .gitignore or use git add -f to force add.

Problem: Accidentally staged wrong files

Solution: Use git reset to unstage files, or git reset <file> to unstage specific file.

Problem: Want to stage only part of a file

Solution: Use git add -p to enter interactive mode and select specific hunks to stage.

Examples

Stage specific file

git add file.txt

Stage all changes in current directory

git add .

Stage changes interactively (patch mode)

git add -p

Stage all changes including deletions

git add -A

Stage all changes across entire repository

git add --all

Stage all changes in src directory

git add src/