interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-add.adoc

469 lines · 16625 bytes Raw Blame History
1git-add(1)
2==========
3
4NAME
5----
6git-add - Add file contents to the index
7
8SYNOPSIS
9--------
10[synopsis]
11git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
12 [--edit | -e] [--[no-]all | -A | --[no-]ignore-removal | [--update | -u]] [--sparse]
13 [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
14 [--resolved] [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]]
15 [--] [<pathspec>...]
16
17DESCRIPTION
18-----------
19Add contents of new or changed files to the index. The "index" (also
20known as the "staging area") is what you use to prepare the contents of
21the next commit.
22
23When you run `git commit` without any other arguments, it will only
24commit staged changes. For example, if you've edited `file.c` and want
25to commit your changes to that file, you can run:
26
27 git add file.c
28 git commit
29
30You can also add only part of your changes to a file with `git add -p`.
31
32This command can be performed multiple times before a commit. It only
33adds the content of the specified file(s) at the time the add command is
34run; if you want subsequent changes included in the next commit, then
35you must run `git add` again to add the new content to the index.
36
37The `git status` command can be used to obtain a summary of which
38files have changes that are staged for the next commit.
39
40The `git add` command will not add ignored files by default. You can
41use the `--force` option to add ignored files. If you specify the exact
42filename of an ignored file, `git add` will fail with a list of ignored
43files. Otherwise it will silently ignore the file.
44
45Please see linkgit:git-commit[1] for alternative ways to add content to a
46commit.
47
48
49OPTIONS
50-------
51`<pathspec>...`::
52 Files to add content from. Fileglobs (e.g. `*.c`) can
53 be given to add all matching files. Also a
54 leading directory name (e.g. `dir` to add `dir/file1`
55 and `dir/file2`) can be given to update the index to
56 match the current state of the directory as a whole (e.g.
57 specifying `dir` will record not just a file `dir/file1`
58 modified in the working tree, a file `dir/file2` added to
59 the working tree, but also a file `dir/file3` removed from
60 the working tree). Note that older versions of Git used
61 to ignore removed files; use `--no-all` option if you want
62 to add modified or new files but ignore removed ones.
63+
64For more details about the _<pathspec>_ syntax, see the 'pathspec' entry
65in linkgit:gitglossary[7].
66
67`-n`::
68`--dry-run`::
69 Don't actually add the file(s), just show if they exist and/or will
70 be ignored.
71
72`-v`::
73`--verbose`::
74 Be verbose.
75
76`-f`::
77`--force`::
78 Allow adding otherwise ignored files. The option is also used when
79 `submodule.<name>.ignore=all` is set, but you want to stage an
80 update of the submodule. The `path` to the submodule must be explicitly
81 specified.
82
83`--sparse`::
84 Allow updating index entries outside of the sparse-checkout cone.
85 Normally, `git add` refuses to update index entries whose paths do
86 not fit within the sparse-checkout cone, since those files might
87 be removed from the working tree without warning. See
88 linkgit:git-sparse-checkout[1] for more details.
89
90`-i`::
91`--interactive`::
92 Add modified contents in the working tree interactively to
93 the index. Optional path arguments may be supplied to limit
94 operation to a subset of the working tree. See ``Interactive
95 mode'' for details.
96
97`-p`::
98`--patch`::
99 Interactively choose hunks of patch between the index and the
100 work tree and add them to the index. This gives the user a chance
101 to review the difference before adding modified contents to the
102 index.
103+
104This effectively runs `add --interactive`, but bypasses the
105initial command menu and directly jumps to the `patch` subcommand.
106See ``Interactive mode'' for details.
107
108include::diff-context-options.adoc[]
109
110`-e`::
111`--edit`::
112 Open the diff vs. the index in an editor and let the user
113 edit it. After the editor was closed, adjust the hunk headers
114 and apply the patch to the index.
115+
116The intent of this option is to pick and choose lines of the patch to
117apply, or even to modify the contents of lines to be staged. This can be
118quicker and more flexible than using the interactive hunk selector.
119However, it is easy to confuse oneself and create a patch that does not
120apply to the index. See <<EDITING_PATCHES,EDITING PATCHES>> below.
121
122`-u`::
123`--update`::
124 Update the index just where it already has an entry matching
125 _<pathspec>_. This removes as well as modifies index entries to
126 match the working tree, but adds no new files.
127+
128If no _<pathspec>_ is given when `-u` option is used, all
129tracked files in the entire working tree are updated (old versions
130of Git used to limit the update to the current directory and its
131subdirectories).
132
133`-A`::
134`--all`::
135`--no-ignore-removal`::
136 Update the index not only where the working tree has a file
137 matching _<pathspec>_ but also where the index already has an
138 entry. This adds, modifies, and removes index entries to
139 match the working tree.
140+
141If no _<pathspec>_ is given when `-A` option is used, all
142files in the entire working tree are updated (old versions
143of Git used to limit the update to the current directory and its
144subdirectories).
145
146`--no-all`::
147`--ignore-removal`::
148 Update the index by adding new files that are unknown to the
149 index and files modified in the working tree, but ignore
150 files that have been removed from the working tree. This
151 option is a no-op when no _<pathspec>_ is used.
152+
153This option is primarily to help users who are used to older
154versions of Git, whose `git add <pathspec>...` was a synonym
155for `git add --no-all <pathspec>...`, i.e. ignored removed files.
156
157`-N`::
158`--intent-to-add`::
159 Record only the fact that the path will be added later. An entry
160 for the path is placed in the index with no content. This is
161 useful for, among other things, showing the unstaged content of
162 such files with `git diff` and committing them with `git commit
163 -a`.
164
165`--refresh`::
166 Don't add the file(s), but only refresh their stat()
167 information in the index.
168
169`--ignore-errors`::
170 If some files could not be added because of errors indexing
171 them, do not abort the operation, but continue adding the
172 others. The command shall still exit with non-zero status.
173 The configuration variable `add.ignoreErrors` can be set to
174 true to make this the default behaviour.
175
176`--ignore-missing`::
177 This option can only be used together with `--dry-run`. By using
178 this option the user can check if any of the given files would
179 be ignored, no matter if they are already present in the work
180 tree or not.
181
182`--no-warn-embedded-repo`::
183 By default, `git add` will warn when adding an embedded
184 repository to the index without using `git submodule add` to
185 create an entry in `.gitmodules`. This option will suppress the
186 warning (e.g., if you are manually performing operations on
187 submodules).
188
189`--renormalize`::
190 Apply the "clean" process freshly to all tracked files to
191 forcibly add them again to the index. This is useful after
192 changing `core.autocrlf` configuration or the `text` attribute
193 in order to correct files added with wrong _CRLF/LF_ line endings.
194 This option implies `-u`. Lone CR characters are untouched, thus
195 while a _CRLF_ cleans to _LF_, a _CRCRLF_ sequence is only partially
196 cleaned to _CRLF_.
197
198`--resolved`::
199 Update the index for unmerged paths matching _<pathspec>_ where
200 no conflict markers remain in the working tree. Unmerged paths
201 without conflict markers (including binary files and file
202 deletions) are staged as resolved, while any path with leftover
203 conflict markers causes the command to refuse to stage any files.
204 Cannot be combined with `-u` or `-A`.
205
206`--chmod=(+|-)x`::
207 Override the executable bit of the added files. The executable
208 bit is only changed in the index, the files on disk are left
209 unchanged.
210
211`--pathspec-from-file=<file>`::
212 Pathspec is passed in _<file>_ instead of commandline args. If
213 _<file>_ is exactly `-` then standard input is used. Pathspec
214 elements are separated by _LF_ or _CR/LF_. Pathspec elements can be
215 quoted as explained for the configuration variable `core.quotePath`
216 (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
217 global `--literal-pathspecs`.
218
219`--pathspec-file-nul`::
220 Only meaningful with `--pathspec-from-file`. Pathspec elements are
221 separated with _NUL_ character and all other characters are taken
222 literally (including newlines and quotes).
223
224`--`::
225 This option can be used to separate command-line options from
226 the list of files (useful when filenames might be mistaken
227 for command-line options).
228
229
230EXAMPLES
231--------
232
233* Adds content from all ++*.txt++ files under `Documentation` directory
234 and its subdirectories:
235+
236------------
237$ git add Documentation/\*.txt
238------------
239+
240Note that the asterisk ++*++ is quoted from the shell in this
241example; this lets the command include the files from
242subdirectories of `Documentation/` directory.
243
244* Considers adding content from all ++git-*.sh++ scripts:
245+
246------------
247$ git add git-*.sh
248------------
249+
250Because this example lets the shell expand the asterisk (i.e. you are
251listing the files explicitly), it does not consider
252`subdir/git-foo.sh`.
253
254INTERACTIVE MODE
255----------------
256When the command enters the interactive mode, it shows the
257output of the 'status' subcommand, and then goes into its
258interactive command loop.
259
260The command loop shows the list of subcommands available, and
261gives a prompt "What now> ". In general, when the prompt ends
262with a single '>', you can pick only one of the choices given
263and type return, like this:
264
265------------
266 *** Commands ***
267 1: status 2: update 3: revert 4: add untracked
268 5: patch 6: diff 7: quit 8: help
269 What now> 1
270------------
271
272You also could say `s` or `sta` or `status` above as long as the
273choice is unique.
274
275The main command loop has 6 subcommands (plus help and quit).
276
277status::
278
279 This shows the change between `HEAD` and index (i.e. what will be
280 committed if you say `git commit`), and between index and
281 working tree files (i.e. what you could stage further before
282 `git commit` using `git add`) for each path. A sample output
283 looks like this:
284+
285------------
286 staged unstaged path
287 1: binary nothing foo.png
288 2: +403/-35 +1/-1 add-interactive.c
289------------
290+
291It shows that `foo.png` has differences from `HEAD` (but that is
292binary so line count cannot be shown) and there is no
293difference between indexed copy and the working tree
294version (if the working tree version were also different,
295'binary' would have been shown in place of 'nothing'). The
296other file, `add-interactive.c`, has 403 lines added
297and 35 lines deleted if you commit what is in the index, but
298working tree file has further modifications (one addition and
299one deletion).
300
301update::
302
303 This shows the status information and issues an "Update>>"
304 prompt. When the prompt ends with double '>>', you can
305 make more than one selection, concatenated with whitespace or
306 comma. Also you can say ranges. E.g. "2-5 7,9" to choose
307 2,3,4,5,7,9 from the list. If the second number in a range is
308 omitted, all remaining patches are taken. E.g. "7-" to choose
309 7,8,9 from the list. You can say '*' to choose everything.
310+
311What you chose are then highlighted with '*',
312like this:
313+
314------------
315 staged unstaged path
316 1: binary nothing foo.png
317* 2: +403/-35 +1/-1 add-interactive.c
318------------
319+
320To remove selection, prefix the input with `-`
321like this:
322+
323------------
324Update>> -2
325------------
326+
327After making the selection, answer with an empty line to stage the
328contents of working tree files for selected paths in the index.
329
330revert::
331
332 This has a very similar UI to 'update', and the staged
333 information for selected paths are reverted to that of the
334 HEAD version. Reverting new paths makes them untracked.
335
336add untracked::
337
338 This has a very similar UI to 'update' and
339 'revert', and lets you add untracked paths to the index.
340
341patch::
342
343 This lets you choose one path out of a 'status' like selection.
344 After choosing the path, it presents the diff between the index
345 and the working tree file and asks you if you want to stage
346 the change of each hunk. You can select one of the following
347 options and type return:
348
349 y - stage this hunk
350 n - do not stage this hunk
351 q - quit; do not stage this hunk or any of the remaining ones
352 a - stage this hunk and all later hunks in the file
353 d - do not stage this hunk or any of the later hunks in the file
354 g - select a hunk to go to
355 / - search for a hunk matching the given regex
356 j - go to the next undecided hunk, roll over at the bottom
357 J - go to the next hunk, roll over at the bottom
358 k - go to the previous undecided hunk, roll over at the top
359 K - go to the previous hunk, roll over at the top
360 s - split the current hunk into smaller hunks
361 e - manually edit the current hunk
362 p - print the current hunk
363 P - print the current hunk using the pager
364 ? - print help
365+
366After deciding the fate for all hunks, if there is any hunk
367that was chosen, the index is updated with the selected hunks.
368+
369You can omit having to type return here, by setting the configuration
370variable `interactive.singleKey` to `true`.
371
372diff::
373
374 This lets you review what will be committed (i.e. between
375 `HEAD` and index).
376
377
378[[EDITING_PATCHES]]
379EDITING PATCHES
380---------------
381
382Invoking `git add -e` or selecting `e` from the interactive hunk
383selector will open a patch in your editor; after the editor exits, the
384result is applied to the index. You are free to make arbitrary changes
385to the patch, but note that some changes may have confusing results, or
386even result in a patch that cannot be applied. If you want to abort the
387operation entirely (i.e., stage nothing new in the index), simply delete
388all lines of the patch. The list below describes some common things you
389may see in a patch, and which editing operations make sense on them.
390
391--
392added content::
393
394Added content is represented by lines beginning with "{plus}". You can
395prevent staging any addition lines by deleting them.
396
397removed content::
398
399Removed content is represented by lines beginning with "-". You can
400prevent staging their removal by converting the "-" to a " " (space).
401
402modified content::
403
404Modified content is represented by "-" lines (removing the old content)
405followed by "{plus}" lines (adding the replacement content). You can
406prevent staging the modification by converting "-" lines to " ", and
407removing "{plus}" lines. Beware that modifying only half of the pair is
408likely to introduce confusing changes to the index.
409--
410
411There are also more complex operations that can be performed. But beware
412that because the patch is applied only to the index and not the working
413tree, the working tree will appear to "undo" the change in the index.
414For example, introducing a new line into the index that is in neither
415the `HEAD` nor the working tree will stage the new line for commit, but
416the line will appear to be reverted in the working tree.
417
418Avoid using these constructs, or do so with extreme caution.
419
420--
421removing untouched content::
422
423Content which does not differ between the index and working tree may be
424shown on context lines, beginning with a " " (space). You can stage
425context lines for removal by converting the space to a "-". The
426resulting working tree file will appear to re-add the content.
427
428modifying existing content::
429
430One can also modify context lines by staging them for removal (by
431converting " " to "-") and adding a "{plus}" line with the new content.
432Similarly, one can modify "{plus}" lines for existing additions or
433modifications. In all cases, the new modification will appear reverted
434in the working tree.
435
436new content::
437
438You may also add new content that does not exist in the patch; simply
439add new lines, each starting with "{plus}". The addition will appear
440reverted in the working tree.
441--
442
443There are also several operations which should be avoided entirely, as
444they will make the patch impossible to apply:
445
446* adding context (" ") or removal ("-") lines
447* deleting context or removal lines
448* modifying the contents of context or removal lines
449
450CONFIGURATION
451-------------
452
453include::includes/cmd-config-section-all.adoc[]
454
455:git-add: 1
456include::config/add.adoc[]
457
458SEE ALSO
459--------
460linkgit:git-status[1]
461linkgit:git-rm[1]
462linkgit:git-reset[1]
463linkgit:git-mv[1]
464linkgit:git-commit[1]
465linkgit:git-update-index[1]
466
467GIT
468---
469Part of the linkgit:git[1] suite