Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
- gitattributes
- Command-line interface conventions
- Everyday Git
- Frequently Asked Questions (FAQ)
- Glossary
- Hooks
- gitignore
- gitmodules
- Revisions
- Submodules
- Tutorial
- Workflows
- All guides...
Administration
Plumbing Commands
- 2.48.1 no changes
- 2.48.0 01/10/25
- 2.46.1 → 2.47.2 no changes
- 2.46.0 no changes
- 2.45.1 → 2.45.3 no changes
- 2.45.0 04/29/24
- 2.43.2 → 2.44.3 no changes
- 2.43.1 02/09/24
- 2.40.1 → 2.43.0 no changes
- 2.40.0 03/12/23
- 2.38.1 → 2.39.5 no changes
- 2.38.0 10/02/22
- 2.37.4 → 2.37.7 no changes
- 2.37.3 08/30/22
- 2.34.1 → 2.37.2 no changes
- 2.34.0 11/15/21
- 2.25.1 → 2.33.8 no changes
- 2.25.0 01/13/20
- 2.22.1 → 2.24.4 no changes
- 2.22.0 no changes
- 2.21.1 → 2.21.4 no changes
- 2.21.0 02/24/19
- 2.18.1 → 2.20.5 no changes
- 2.18.0 06/21/18
- 2.17.0 → 2.17.6 no changes
- 2.16.6 12/06/19
- 2.15.4 no changes
- 2.14.6 12/06/19
- 2.11.4 → 2.13.7 no changes
- 2.10.5 09/22/17
- 2.8.6 → 2.9.5 no changes
- 2.7.6 07/30/17
- 2.5.6 → 2.6.7 no changes
- 2.4.12 05/05/17
- 2.3.10 09/28/15
- 2.1.4 → 2.2.3 no changes
- 2.0.5 12/17/14
概述
git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [--edit | -e] [--[no-]all | -A | --[no-]ignore-removal | [--update | -u]] [--sparse] [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize] [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>…]
描述
该命令使用工作区的内容来更新索引,为下一次提交准备暂存的内容。通常情况下会添加当前目录的所有内容到索引,但是通过某些选项,它也可以用于仅添加对工作树文件所做的部分更改,或删除工作树中不存在的路径。
索引区中储存着一份工作区的快照,在下次提交时这份快照会被保存到 Git 仓库中。因此在工作区做过修改后以及在运行下一次提交命令前,你必须通过 add
命令来添加新创建的文件或者保存新的改动到索引中。
提交前可以多次执行此命令。它仅在运行add命令时添加指定文件的内容;如果要在下一次提交中包含后续更改,则必须再次运行`git add`将新内容添加到索引中。
git status
命令可用于获取摘要,说明哪些变化的文件已暂存,准备下一次提交。
The git add
command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add
will fail with a list of ignored files. Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored. The git add
command can be used to add ignored files with the -f
(force) option.
请参阅 git-commit[1] 了解将内容添加到提交的其他替代方法。
选项
-
<pathspec>...
-
要从中添加内容的文件。可以使用文件通配符(例如
*.c
)来添加所有匹配的文件。此外,还可以提供一个前导目录名(例如,指定dir
以添加dir/file1
和dir/file2
)来更新索引,使其与整个目录的当前状态相匹配(例如,指定dir
不仅会记录在工作树中修改的文件dir/file1
,还会记录在工作树中添加的文件dir/file2
,还有从工作树中删除的文件dir/file3
)。请注意,旧版本的 Git 会忽略已删除的文件;如果要添加已修改的文件或新文件,但忽略已删除的文件,请使用--no-all
选项。For more details about the <pathspec> syntax, see the pathspec entry in gitglossary[7].
-
-n
-
--dry-run
-
实际上不添加文件,仅展示文件是否存在或是否忽略。
-
-v
-
--verbose
-
详细日志。
-
-f
-
--force
-
允许添加已被忽略的文件。
-
--sparse
-
允许更新稀疏检出 cone 之外的索引条目。 通常,
git add
拒绝更新其路径不适合稀疏检出 cone 的索引条目,因为这些文件可能会在没有警告的情况下从工作区中删除。有关更多详细信息,请参阅链接 git-sparse-checkout[1]。 -
-i
-
--interactive
-
以交互方式将工作目录树中的修改内容添加到索引。可以提供可选的路径参数,以将操作限制为工作目录树的子集。有关详细信息,请参见 '交互模式'。
-
-p
-
--patch
-
交互地在索引和工作目录之间选择补丁块并将它们添加到索引中。这让用户有机会在将修改后的内容添加到索引之前查看差异。
参见这可以有效地运行
add --interactive
,但是会绕过初始命令菜单,而直接跳转到patch
子命令。有关详细信息,请参见 '交互模式'。 -
-e
-
--edit
-
在编辑器中打开与索引的差异,使用户进行编辑。关闭编辑器后,调整块补丁头并将其应用于索引。
此选项的目的是选择并选择要应用的补丁的行,甚至修改要暂存的行的内容。与使用交互式补丁块选择器相比,其更快,更灵活。但是,很容易混淆自己并创建不应用于索引的补丁。请参阅下面的编辑补丁。
-
-u
-
--update
-
Update the index just where it already has an entry matching <pathspec>. This removes as well as modifies index entries to match the working tree, but adds no new files.
If no <pathspec> is given when
-u
option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories). -
-A
-
--all
-
--no-ignore-removal
-
Update the index not only where the working tree has a file matching <pathspec> but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.
If no <pathspec> is given when
-A
option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories). -
--no-all
-
--ignore-removal
-
Update the index by adding new files that are unknown to the index and files modified in the working tree, but ignore files that have been removed from the working tree. This option is a no-op when no <pathspec> is used.
This option is primarily to help users who are used to older versions of Git, whose
git add <pathspec>...
was a synonym forgit add --no-all <pathspec>...
, i.e. ignored removed files. -
-N
-
--intent-to-add
-
只记录稍后将添加路径的事实。路径的项会被放置在索引中,但不包括改动的内容。这对于使用
git diff
显示文件的未暂存内容以及使git commit -a
提交这些文件非常有用。 -
--refresh
-
不添加文件,只刷新索引中的 stat() 信息。
-
--ignore-errors
-
如果由于索引错误而无法添加某些文件,请不要中止操作,而是继续添加其他文件。命令仍应以非零状态退出。可以将配置变量
add.ignoreErrors
设置为 true,使其成为默认行为。 -
--ignore-missing
-
This option can only be used together with
--dry-run
. By using this option the user can check if any of the given files would be ignored, no matter if they are already present in the work tree or not. -
--no-warn-embedded-repo
-
默认情况下,
git add
将在向索引添加嵌入式存储库时发出警告,而不使用git submodule add
在.gitmodules
中创建条目。此选项将抑制警告(例如,如果手动对子模块执行操作)。 -
--renormalize
-
Apply the "clean" process freshly to all tracked files to forcibly add them again to the index. This is useful after changing
core.autocrlf
configuration or thetext
attribute in order to correct files added with wrong CRLF/LF line endings. This option implies-u
. Lone CR characters are untouched, thus while a CRLF cleans to LF, a CRCRLF sequence is only partially cleaned to CRLF. -
--chmod=(+|-)x
-
重写添加文件的可执行位。可执行位仅在索引中更改,磁盘上的文件保持不变。
-
--pathspec-from-file=<file>
-
Pathspec is passed in <file> instead of commandline args. If <file> is exactly
-
then standard input is used. Pathspec elements are separated by LF or CR/LF. Pathspec elements can be quoted as explained for the configuration variablecore.quotePath
(see git-config[1]). See also--pathspec-file-nul
and global--literal-pathspecs
. -
--pathspec-file-nul
-
Only meaningful with
--pathspec-from-file
. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes). -
--
-
此选项可用于分离命令行选项和文件列表(当文件名可能被误认为命令行选项时非常有用)。
实例
-
Adds content from all
*.txt
files underDocumentation
directory and its subdirectories:$ git add Documentation/\*.txt
Note that the asterisk
*
is quoted from the shell in this example; this lets the command include the files from subdirectories ofDocumentation/
directory. -
Considers adding content from all
git-*.sh
scripts:$ git add git-*.sh
因为这个示例允许 shell 扩展星号(即显式列出文件),所以它不包含
subdir/git-foo.sh
。
交互模式
当命令进入交互模式时,它将显示 status
子命令的输出,然后进入其交互命令循环。
命令循环显示可用的子命令列表,并提示 "What now>"。通常,当提示以单个 > 结尾时,您只能选择给定的一个选项并键入回车,如下所示:
*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> 1
你也可以输入 s
、sta
或 status
,只要候选项是唯一的。
主命令循环有 6 个子命令(包括帮助和退出)。
- 状态
-
This shows the change between
HEAD
and index (i.e. what will be committed if you saygit commit
), and between index and working tree files (i.e. what you could stage further beforegit commit
usinggit add
) for each path. A sample output looks like this:staged unstaged path 1: binary nothing foo.png 2: +403/-35 +1/-1 git-add--interactive.perl
It shows that
foo.png
has differences fromHEAD
(but that is binary so line count cannot be shown) and there is no difference between indexed copy and the working tree version (if the working tree version were also different, binary would have been shown in place of nothing). The other file,add-interactive.c
, has 403 lines added and 35 lines deleted if you commit what is in the index, but working tree file has further modifications (one addition and one deletion). - update
-
这将显示状态信息并发出 "Update>>" 提示。当提示以 >> 结尾时,可以进行多个选择,并用空格或逗号连接。你也可以输入范围。例如:"2-5 7,9" 将从列表中选择 2、3、4、5、7、9。如果省略一个范围中的第二个数字,则将获取所有剩余的补丁。例如:"7-" 从列表中选择 7、8、9。你可以输入 * 来选择全部补丁。
您选择的内容会用 * 突出显示,如下所示:
staged unstaged path 1: binary nothing foo.png * 2: +403/-35 +1/-1 git-add--interactive.perl
要删除选定内容,请在输入前加上
-
,如下所示:Update>> -2
进行选择后,输入空行,以便为索引中选定的路径暂存工作目录文件的内容。
- 还原
-
它有一个与 update 非常相似的交互过程,所选路径的暂存信息将还原为 HEAD 版本。还原新路径将使它们不被追踪。
- 添加未跟踪文件
-
它有一个与 update 和 revert 非常相似的交互过程,允许您向索引添加未跟踪的路径。
- 补丁
-
这允许您从类似 status 命令的选择中选择一条路径。在选择路径之后,它将显示索引和工作树文件之间的差异,并询问您是否要暂存每个块的更改。您可以选择以下选项之一并输入回车:
y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk or any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk or any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk p - print the current hunk ? - print help
在决定了所有块的操作后,如果有选择的区块,则会使用其来更新索引。
通过将配置变量
interactive.singleKey
设置为true
,在此处可以不必键入回车。 - diff
-
This lets you review what will be committed (i.e. between
HEAD
and index).
编辑补丁
调用 git add -e
或从交互式块选择器中选择 e
命令,将在编辑器中打开一个补丁。编辑器退出后,结果将应用于索引。您可以随意对补丁进行任何更改,但是请注意,某些更改可能会导致混乱,甚至导致补丁无法使用。如果您想完全中止该操作(即不在索引中更新内容),只需删除补丁中的所有行。下面的列表描述了您可能会在补丁中看到的一些常见内容,以及对其进行哪些编辑操作才有意义。
There are also more complex operations that can be performed. But beware that because the patch is applied only to the index and not the working tree, the working tree will appear to "undo" the change in the index. For example, introducing a new line into the index that is in neither the HEAD
nor the working tree will stage the new line for commit, but the line will appear to be reverted in the working tree.
避免使用这些构造,除非格外小心。
还有一些操作应完全避免,因为它们会使补丁无法应用:
-
添加上下文(" ")行或添加删除("-")行
-
删除上下文行或删除标记为删除的行
-
修改上下文或删除行的内容
配置
本节中这一行以下的内容都是从 git-config[1] 文档中摘录的。其内容与那里的内容相同:
Warning
|
Missing See original version for this content. |
GIT
属于 git[1] 文档