THE GITHTML FIELD GUIDE

Stage only the files you intend to commit

Stage specific paths with git add -- followed by the files you reviewed, then inspect git diff --staged before committing. Avoid treating every modified or untracked file as part of the task. Staging updates the index with the selected content; it does not upload files or require other working changes to be discarded.

Choose the logical change first

The boundary of a commit is a decision about meaning, not a convenience offered by the terminal. A bug fix might include a source file and its focused test while excluding a local debug log, an unrelated formatting edit, and a teammate’s unfinished work. Decide what the commit should accomplish before choosing which paths to add.

Inspect status and the ordinary diff to identify changed files. Read untracked files separately before adding them because they can include credentials, generated exports, or large accidental downloads. If a path contains both the intended fix and unrelated edits, file-level staging is too broad; use patch staging or separate the edits deliberately.

Add explicit paths and review the index

The example assumes parser.js and parser.test.js belong together. Quoting paths with spaces prevents the shell from splitting them, while -- marks the start of path arguments. Run from the repository root or use paths relative to your current directory. The final command reveals exactly what a normal commit would record.

  1. Compare the staged file list with the task scope. A file being modified before you started is a reason to inspect carefully, not permission to include it.
  2. If you staged an unintended path in a repository with commits, git restore --staged -- path removes its changes from the proposed commit without deleting working edits.
shell
git status --short
git diff -- parser.js parser.test.js
git add -- parser.js parser.test.js
git diff --staged

Keep excluded work visible and safe

A successful focused commit can leave the working tree dirty. That is not a failure when the remaining changes belong to another task or another person. Confirm the commit with git show --stat and inspect status again so the remaining state is understood. Do not clean or reset unrelated files merely to produce an empty status display.

Be cautious with broad add commands, especially from a directory whose contents you have not reviewed. They can include new files and removals beyond the intended fix. Explicit paths make the scope visible in shell history and review notes, although they are not a substitute for reading the staged patch. The useful habit is selection followed by verification: choose the smallest coherent snapshot, then check that the index actually matches that choice.

Named paths also make accidental deletions easier to spot. Review removed files with the same care as additions; both are real changes in the snapshot.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All git fundamentals guides →

Read as MarkdownAll guides

Stage only the files you intend to commit · gitHtml