Skip to content

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.

Terminal window
git add app/User.php

Git 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 add
Working Tree → latest editor version

Running 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:

Terminal window
git add tests/UserTest.php

Stage changes inside a directory and its descendants:

Terminal window
git add tests/

Stage changes from your current directory downward:

Terminal window
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.

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.

Terminal window
git add -p

This 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.

Suppose a Laravel test is ready but a configuration edit is still exploratory. Stage the test path explicitly, then inspect status:

Terminal window
git add tests/UserTest.php
git status

Confirm 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.

git add <file> → stage current content of one path
git add <dir> → stage changes under a directory
git add . → stage from the current directory downward
git add -A → stage new, modified, and deleted paths repository-wide
git add -u → stage modified and deleted currently tracked paths
git add -p → interactively select displayed change hunks
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.