Staging Files
The Working Tree can contain more changes than you want in the next commit. Staging lets you choose the current content of particular paths without recording a commit yet. This helps keep a backend fix separate from unrelated experiments.
1. What git add actually selects
Section titled “1. What git add actually selects”git add app/User.phpGit updates the Index (Staging Area) with the current content of that path. It does not create a commit. If you edit the file again afterward, the Index still holds the earlier content until you stage the path again:
Index → version at the most recent git addWorking Tree → latest editor versionRunning git add app/User.php again updates the staged version to the current
file content. A staged new file is already known to the Index even before the
first commit.
2. Scope: file, directory, or current location
Section titled “2. Scope: file, directory, or current location”Stage one path:
git add tests/UserTest.phpStage changes inside a directory and its descendants:
git add tests/Stage changes from your current directory downward:
git add .The dot does not always mean the whole repository. From the repository root,
it covers the whole project. From inside tests/, it covers tests/ and its
descendants, not sibling app/ files. Check your current location before
using this form. Within the selected scope, modern Git stages new files,
tracked modifications, and tracked deletions.
3. Repository-wide forms
Section titled “3. Repository-wide forms”With no path argument, these commands apply across the repository regardless of your current subdirectory:
| Command | New untracked files | Modified tracked files | Deleted tracked files |
|---|---|---|---|
git add -A |
Yes | Yes | Yes |
git add -u |
No | Yes | Yes |
-u means --update: update paths Git currently tracks. It does not mean
“only paths that have already been committed.” A new file staged by -A is
already in the Index; if you edit it again, -u can update its staged content.
In a disposable exercise, two new files stayed untracked after git add -u.
git add -A then staged both. After App/user.php was edited again, another
git add -u updated its staged content and cleared the newer unstaged change.
The files still appeared as new file because the repository had no commit
yet; the final -u had not created them from scratch in the Index.
4. Partial staging, as a signpost
Section titled “4. Partial staging, as a signpost”git add -pThis asks whether to stage each displayed change hunk. It is useful when a
single file contains a finished fix and an unrelated experiment. A hunk can
contain several nearby changed lines: answering y selects the displayed
hunk, not one line. Answer n to leave it unstaged, q to stop, or ? for
Git’s prompt help. Splitting and manually editing hunks are covered later in
the dedicated Patch Mode checkpoint. Do not manually edit a patch without
understanding its format and reviewing the result.
5. A safe backend workflow
Section titled “5. A safe backend workflow”Suppose a Laravel test is ready but a configuration edit is still exploratory. Stage the test path explicitly, then inspect status:
git add tests/UserTest.phpgit statusConfirm that only the intended content is staged. Before a broad -A or
git add ., check your location and status so an unrelated file does not
enter the next commit. Staging is reversible, but broad selection still makes
review harder and can accidentally include sensitive project files.
6. Summary
Section titled “6. Summary”git add <file> → stage current content of one pathgit add <dir> → stage changes under a directorygit add . → stage from the current directory downwardgit add -A → stage new, modified, and deleted paths repository-widegit add -u → stage modified and deleted currently tracked pathsgit add -p → interactively select displayed change hunks7. Knowledge check
Section titled “7. Knowledge check”What happens if you edit a file after staging it?
The new edit remains in the Working Tree until that content is staged again.
Does git add . from inside tests/ include a sibling app/ directory?
No. The dot selects the current directory and its descendants.
Why did git add -u ignore two new files but later update one of them?
The files were initially untracked. Another command staged them first, so the later edit to one path was an update to content already in the Index.