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.
git add [<options>] [--] [<pathspec>...]
Try it
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.
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
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 andgit add -uwas scoped to the current directory. Todaygit add .stages deletions under the current directory, and-Aand-uact on the whole tree regardless of where you stand. - Staging is a snapshot. Editing a file after
git addleaves two versions: the staged and the working one, shown asMMin 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 addwrites 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
.gitattributesare 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
-uto avoid scanning untracked trees.