git add

Staging with -u and -A, and reproducible hunk selection with git add -p, checked on Git 2.39.5.

命令页正文目前为英文。

git add copies content from the working tree into the index for the next commit. This page covers the difference between updating tracked paths and adding untracked files, and how to stage part of a file with --patch. It is not a tutorial on writing a first commit; see the catalog and the snapshot chapter for that.

Examples were checked on Git 2.39.5 in isolated temporary repositories. git add -p is interactive; the recorded session feeds s, y, and n on standard input so the result is reproducible.

Examples were checked against Git 2.39.5. See the official documentation for the complete command.

Git command catalog → · Git tutorial overview →

Update tracked files versus add all

git add -u stages modifications and deletions of already tracked paths, and it leaves untracked files unstaged. git add -A stages those same tracked updates and also stages new untracked files. git add . is similar to -A, but it is limited to the current directory and its subdirectories.

The example uses git restore --staged . only to unstage and show -A afterwards. That form restores the index from HEAD and does not overwrite working tree files. Review status before you add a whole tree, and do not stage secrets or generated files by accident.

git add -u, git add -A

git add [<options>] [--] <pathspec>...
git add -u
git add -A

Stage tracked updates and deletions with -u, or include untracked files as well with -A.

ParameterDescription
-u / --updateStage modifications and removals of tracked files. Untracked files are not added.
-A / --allStage modifications, removals, and untracked files in the repository (subject to pathspec and ignore rules).

Returns: The index is updated. Follow with git status --porcelain=v1 to inspect XY codes: M and D in the first column after a successful update, and A for a newly staged file.

  • The restore --staged step in the script is an unstage, not a working-tree discard.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'a0\n' > a.txt
printf 'b0\n' > b.txt
git add a.txt b.txt
git commit -m "Base" >/dev/null
printf 'a1\n' > a.txt
printf 'b1\n' > b.txt
printf 'c0\n' > c.txt
rm b.txt
echo "before"
git status --porcelain=v1
git add -u
echo "after-update"
git status --porcelain=v1
git restore --staged .
echo "unstaged"
git status --porcelain=v1
git add -A
echo "after-all"
git status --porcelain=v1

Expected output

before
 M a.txt
 D b.txt
?? c.txt
after-update
M  a.txt
D  b.txt
?? c.txt
unstaged
 M a.txt
 D b.txt
?? c.txt
after-all
M  a.txt
D  b.txt
A  c.txt

Related: git status, git restore

Official documentation →

Partial staging with --patch

git add -p, also --patch, presents each change hunk and waits for a letter. s splits a hunk when Git can divide it, y stages the current hunk, and n skips it. Other letters include a (stage this and later hunks in the file), d (skip this and later hunks), and q (quit).

The example changes two places in one file, which Git first offers as a single hunk. Feeding s then y then n splits that hunk, stages the line2 edit, and leaves the extra line unstaged. The result is MM in porcelain: the index and the working tree both differ from each other and from HEAD.

git add -p

git add -p [--] [<pathspec>...]
git add --patch

Interactively split and stage selected hunks of a file.

ParameterDescription
-p / --patchReview hunks interactively. The example answers s, y, and n on standard input.
<pathspec>Optional paths to limit the session. A -- separator is used before the file name.

Returns: The index contains only the hunks you accepted. The working tree still has the skipped edits.

  • This command is interactive. Do not pipe answers into it unless you have reviewed the hunks, as this example does for a known file.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'line1\nline2\nline3\n' > f.txt
git add f.txt
git commit -m "Three lines" >/dev/null
printf 'line1\nLINE2\nline3\nextra\n' > f.txt
printf 's\ny\nn\n' | git add -p -- f.txt
echo "porcelain"
git status --porcelain=v1
echo "cached"
git diff --cached --unified=1
echo "worktree"
git diff --unified=1

Expected output

diff --git a/f.txt b/f.txt
index 83db48f..fcbcda4 100644
--- a/f.txt
+++ b/f.txt
@@ -1,3 +1,4 @@
 line1
-line2
+LINE2
 line3
+extra
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? Split into 2 hunks.
@@ -1,3 +1,3 @@
 line1
-line2
+LINE2
 line3
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? @@ -3 +3,2 @@
 line3
+extra
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? 
porcelain
MM f.txt
cached
diff --git a/f.txt b/f.txt
index 83db48f..534a16e 100644
--- a/f.txt
+++ b/f.txt
@@ -1,3 +1,3 @@
 line1
-line2
+LINE2
 line3
worktree
diff --git a/f.txt b/f.txt
index 534a16e..fcbcda4 100644
--- a/f.txt
+++ b/f.txt
@@ -3 +3,2 @@ LINE2
 line3
+extra

Official documentation →