git add is the most frequent Git command after git status. It handles
the transition from "edits on disk" to "edits ready to commit".
What happens under the hood
For each file:
- Reads the content from the working tree.
- Creates a blob object in
.git/objects/. - Updates the record in
.git/index: path, permissions, blob SHA.
After this, git status shows the file under "Changes to be committed".
Basic forms
git add file.txt # single file
git add src/ # directory, recursively
git add . # everything from the current directory
git add -A # all changes across the entire repository
git add '*.py' # by glob pattern
The difference between . and -A: the dot adds only from the current
working directory; -A looks at the whole repository regardless of where
you are.
Interactive mode
git add -p file.txt
Splits the edit into hunks (chunks with ±3 lines of context) and asks
about each one: y to add, n to skip, s to split into smaller pieces,
e to edit manually. This is the primary tool for splitting one file that
has two unrelated changes into two separate commits.
Pitfalls
git add file.txtwrites the current contents to the index. If you edit the file again afteradd, the old version stays in the index.git statuswill show the file under both "to be committed" and "not staged". Rungit addagain to catch up.- New files are not tracked by default. They appear as untracked until you add them explicitly. There is no automatic registration. This is intentional: accidental files (logs, build artifacts) should not end up in the index.