Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Staging and Committing

git add command

Reference for git add: stage new, modified and deleted files, use -p for partial staging, -u and -A scopes, intent-to-add, dry runs and ignored files.

git add copies the current content of files from the working tree into the index (the staging area), recording what the next commit will contain. It stages new files, modifications and, in current Git versions, deletions of the paths you name. Nothing is committed until you run git commit.

The content is captured at the moment you run the command: if you edit a file afterwards, the newer edits stay unstaged until you add again. With -p you can stage only some hunks of a file.

Terminal
git add [<options>] [--] [<pathspec>...]

Try it

Bash Scopes: dot, -u and -A

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Intent-to-add, forcing ignored files and patch mode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git add <pathspec>...
git add -u [<pathspec>...]
git add -A
git add -p [<pathspec>...]
Option Description
<pathspec>... Files, directories or pathspec patterns. A directory is staged recursively.
-A, --all Stage every change in the whole working tree: new, modified and deleted.
-u, --update Stage modifications and deletions of tracked files; never adds new files. Without a pathspec it covers the whole tree (since Git 2.0).
--no-all, --ignore-removal Add new and modified files, but ignore deletions.
-p, --patch Choose hunks interactively.
-i, --interactive Menu-driven staging.
-e, --edit Open the diff in your editor to hand-edit what gets staged.
-N, --intent-to-add Record only that the path will exist; its content appears as an addition in git diff.
-f, --force Allow ignored files.
-n, --dry-run Show what would be done without staging.
-v, --verbose Report each added file.
`--chmod=(+ -)x`
--renormalize Re-apply line-ending and filter rules to all tracked files.
--refresh Only refresh stat information, adding nothing.
--ignore-errors Continue past unreadable files.
--sparse Allow updating paths outside the sparse-checkout cone.
--pathspec-from-file=<file>, --pathspec-file-nul Read pathspecs from a file (- for standard input).

Patch mode keys: y stage this hunk, n skip, q quit, a stage this and all later hunks in the file, d skip this and the rest, s split into smaller hunks, e edit the hunk, ? help.

Errors: a pathspec that matches nothing fails with fatal: pathspec 'x' did not match any files. An ignored path is refused unless you pass -f. Files that cannot be read stop the command unless --ignore-errors is used.

Usage notes

  • Version differences. Before Git 2.0, git add . did not stage deletions and git add -u was scoped to the current directory. Today git add . stages deletions under the current directory, and -A and -u act on the whole tree regardless of where you stand.
  • Staging is a snapshot. Editing a file after git add leaves two versions: the staged and the working one, shown as MM in git status.
  • Empty directories cannot be tracked; add a placeholder file such as .gitkeep.
  • Ignored files that are already tracked are still staged normally, because ignore rules only affect untracked paths.
  • Large files and secrets. git add writes the content into the object database immediately. Unstaging does not delete the object right away, so never stage credentials "just for a moment".
  • Line endings and filters from .gitattributes are applied during add (text=auto, clean filters), which is why a warning about CRLF conversion can appear here.
  • Submodules. Adding a directory that contains another repository stages a gitlink (a pinned commit), not its files.
  • Performance. Adding many files is mostly proportional to their size; use pathspecs or -u to avoid scanning untracked trees.