Raw
1 git-add(1)
2 ==========
3
4 NAME
5 ----
6 git-add - Add file contents to the index
7
8 SYNOPSIS
9 --------
10 [synopsis]
11 git 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 [--chmod=(+|-)x] [--pathspec-from-file=<file> [--pathspec-file-nul]]
15 [--] [<pathspec>...]
16
17 DESCRIPTION
18 -----------
19 Add contents of new or changed files to the index. The "index" (also
20 known as the "staging area") is what you use to prepare the contents of
21 the next commit.
22
23 When you run `git commit` without any other arguments, it will only
24 commit staged changes. For example, if you've edited `file.c` and want
25 to commit your changes to that file, you can run:
26
27 git add file.c
28 git commit
29
30 You can also add only part of your changes to a file with `git add -p`.
31
32 This command can be performed multiple times before a commit. It only
33 adds the content of the specified file(s) at the time the add command is
34 run; if you want subsequent changes included in the next commit, then
35 you must run `git add` again to add the new content to the index.
36
37 The `git status` command can be used to obtain a summary of which
38 files have changes that are staged for the next commit.
39
40 The `git add` command will not add ignored files by default. You can
41 use the `--force` option to add ignored files. If you specify the exact
42 filename of an ignored file, `git add` will fail with a list of ignored
43 files. Otherwise it will silently ignore the file.
44
45 Please see linkgit:git-commit[1] for alternative ways to add content to a
46 commit.
47
48
49 OPTIONS
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 +
64 For more details about the _<pathspec>_ syntax, see the 'pathspec' entry
65 in 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 +
104 This effectively runs `add --interactive`, but bypasses the
105 initial command menu and directly jumps to the `patch` subcommand.
106 See ``Interactive mode'' for details.
107
108 include::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 +
116 The intent of this option is to pick and choose lines of the patch to
117 apply, or even to modify the contents of lines to be staged. This can be
118 quicker and more flexible than using the interactive hunk selector.
119 However, it is easy to confuse oneself and create a patch that does not
120 apply to the index. See 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 +
128 If no _<pathspec>_ is given when `-u` option is used, all
129 tracked files in the entire working tree are updated (old versions
130 of Git used to limit the update to the current directory and its
131 subdirectories).
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 +
141 If no _<pathspec>_ is given when `-A` option is used, all
142 files in the entire working tree are updated (old versions
143 of Git used to limit the update to the current directory and its
144 subdirectories).
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 +
153 This option is primarily to help users who are used to older
154 versions of Git, whose `git add <pathspec>...` was a synonym
155 for `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 `--chmod=(+|-)x`::
199 Override the executable bit of the added files. The executable
200 bit is only changed in the index, the files on disk are left
201 unchanged.
202
203 `--pathspec-from-file=<file>`::
204 Pathspec is passed in _<file>_ instead of commandline args. If
205 _<file>_ is exactly `-` then standard input is used. Pathspec
206 elements are separated by _LF_ or _CR/LF_. Pathspec elements can be
207 quoted as explained for the configuration variable `core.quotePath`
208 (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
209 global `--literal-pathspecs`.
210
211 `--pathspec-file-nul`::
212 Only meaningful with `--pathspec-from-file`. Pathspec elements are
213 separated with _NUL_ character and all other characters are taken
214 literally (including newlines and quotes).
215
216 `--`::
217 This option can be used to separate command-line options from
218 the list of files, (useful when filenames might be mistaken
219 for command-line options).
220
221
222 EXAMPLES
223 --------
224
225 * Adds content from all ++*.txt++ files under `Documentation` directory
226 and its subdirectories:
227 +
228 ------------
229 $ git add Documentation/\*.txt
230 ------------
231 +
232 Note that the asterisk ++*++ is quoted from the shell in this
233 example; this lets the command include the files from
234 subdirectories of `Documentation/` directory.
235
236 * Considers adding content from all ++git-*.sh++ scripts:
237 +
238 ------------
239 $ git add git-*.sh
240 ------------
241 +
242 Because this example lets the shell expand the asterisk (i.e. you are
243 listing the files explicitly), it does not consider
244 `subdir/git-foo.sh`.
245
246 INTERACTIVE MODE
247 ----------------
248 When the command enters the interactive mode, it shows the
249 output of the 'status' subcommand, and then goes into its
250 interactive command loop.
251
252 The command loop shows the list of subcommands available, and
253 gives a prompt "What now> ". In general, when the prompt ends
254 with a single '>', you can pick only one of the choices given
255 and type return, like this:
256
257 ------------
258 *** Commands ***
259 1: status 2: update 3: revert 4: add untracked
260 5: patch 6: diff 7: quit 8: help
261 What now> 1
262 ------------
263
264 You also could say `s` or `sta` or `status` above as long as the
265 choice is unique.
266
267 The main command loop has 6 subcommands (plus help and quit).
268
269 status::
270
271 This shows the change between `HEAD` and index (i.e. what will be
272 committed if you say `git commit`), and between index and
273 working tree files (i.e. what you could stage further before
274 `git commit` using `git add`) for each path. A sample output
275 looks like this:
276 +
277 ------------
278 staged unstaged path
279 1: binary nothing foo.png
280 2: +403/-35 +1/-1 add-interactive.c
281 ------------
282 +
283 It shows that `foo.png` has differences from `HEAD` (but that is
284 binary so line count cannot be shown) and there is no
285 difference between indexed copy and the working tree
286 version (if the working tree version were also different,
287 'binary' would have been shown in place of 'nothing'). The
288 other file, `add-interactive.c`, has 403 lines added
289 and 35 lines deleted if you commit what is in the index, but
290 working tree file has further modifications (one addition and
291 one deletion).
292
293 update::
294
295 This shows the status information and issues an "Update>>"
296 prompt. When the prompt ends with double '>>', you can
297 make more than one selection, concatenated with whitespace or
298 comma. Also you can say ranges. E.g. "2-5 7,9" to choose
299 2,3,4,5,7,9 from the list. If the second number in a range is
300 omitted, all remaining patches are taken. E.g. "7-" to choose
301 7,8,9 from the list. You can say '*' to choose everything.
302 +
303 What you chose are then highlighted with '*',
304 like this:
305 +
306 ------------
307 staged unstaged path
308 1: binary nothing foo.png
309 * 2: +403/-35 +1/-1 add-interactive.c
310 ------------
311 +
312 To remove selection, prefix the input with `-`
313 like this:
314 +
315 ------------
316 Update>> -2
317 ------------
318 +
319 After making the selection, answer with an empty line to stage the
320 contents of working tree files for selected paths in the index.
321
322 revert::
323
324 This has a very similar UI to 'update', and the staged
325 information for selected paths are reverted to that of the
326 HEAD version. Reverting new paths makes them untracked.
327
328 add untracked::
329
330 This has a very similar UI to 'update' and
331 'revert', and lets you add untracked paths to the index.
332
333 patch::
334
335 This lets you choose one path out of a 'status' like selection.
336 After choosing the path, it presents the diff between the index
337 and the working tree file and asks you if you want to stage
338 the change of each hunk. You can select one of the following
339 options and type return:
340
341 y - stage this hunk
342 n - do not stage this hunk
343 q - quit; do not stage this hunk or any of the remaining ones
344 a - stage this hunk and all later hunks in the file
345 d - do not stage this hunk or any of the later hunks in the file
346 g - select a hunk to go to
347 / - search for a hunk matching the given regex
348 j - go to the next undecided hunk, roll over at the bottom
349 J - go to the next hunk, roll over at the bottom
350 k - go to the previous undecided hunk, roll over at the top
351 K - go to the previous hunk, roll over at the top
352 s - split the current hunk into smaller hunks
353 e - manually edit the current hunk
354 p - print the current hunk
355 P - print the current hunk using the pager
356 ? - print help
357 +
358 After deciding the fate for all hunks, if there is any hunk
359 that was chosen, the index is updated with the selected hunks.
360 +
361 You can omit having to type return here, by setting the configuration
362 variable `interactive.singleKey` to `true`.
363
364 diff::
365
366 This lets you review what will be committed (i.e. between
367 `HEAD` and index).
368
369
370 EDITING PATCHES
371 ---------------
372
373 Invoking `git add -e` or selecting `e` from the interactive hunk
374 selector will open a patch in your editor; after the editor exits, the
375 result is applied to the index. You are free to make arbitrary changes
376 to the patch, but note that some changes may have confusing results, or
377 even result in a patch that cannot be applied. If you want to abort the
378 operation entirely (i.e., stage nothing new in the index), simply delete
379 all lines of the patch. The list below describes some common things you
380 may see in a patch, and which editing operations make sense on them.
381
382 --
383 added content::
384
385 Added content is represented by lines beginning with "{plus}". You can
386 prevent staging any addition lines by deleting them.
387
388 removed content::
389
390 Removed content is represented by lines beginning with "-". You can
391 prevent staging their removal by converting the "-" to a " " (space).
392
393 modified content::
394
395 Modified content is represented by "-" lines (removing the old content)
396 followed by "{plus}" lines (adding the replacement content). You can
397 prevent staging the modification by converting "-" lines to " ", and
398 removing "{plus}" lines. Beware that modifying only half of the pair is
399 likely to introduce confusing changes to the index.
400 --
401
402 There are also more complex operations that can be performed. But beware
403 that because the patch is applied only to the index and not the working
404 tree, the working tree will appear to "undo" the change in the index.
405 For example, introducing a new line into the index that is in neither
406 the `HEAD` nor the working tree will stage the new line for commit, but
407 the line will appear to be reverted in the working tree.
408
409 Avoid using these constructs, or do so with extreme caution.
410
411 --
412 removing untouched content::
413
414 Content which does not differ between the index and working tree may be
415 shown on context lines, beginning with a " " (space). You can stage
416 context lines for removal by converting the space to a "-". The
417 resulting working tree file will appear to re-add the content.
418
419 modifying existing content::
420
421 One can also modify context lines by staging them for removal (by
422 converting " " to "-") and adding a "{plus}" line with the new content.
423 Similarly, one can modify "{plus}" lines for existing additions or
424 modifications. In all cases, the new modification will appear reverted
425 in the working tree.
426
427 new content::
428
429 You may also add new content that does not exist in the patch; simply
430 add new lines, each starting with "{plus}". The addition will appear
431 reverted in the working tree.
432 --
433
434 There are also several operations which should be avoided entirely, as
435 they will make the patch impossible to apply:
436
437 * adding context (" ") or removal ("-") lines
438 * deleting context or removal lines
439 * modifying the contents of context or removal lines
440
441 CONFIGURATION
442 -------------
443
444 include::includes/cmd-config-section-all.adoc[]
445
446 :git-add: 1
447 include::config/add.adoc[]
448
449 SEE ALSO
450 --------
451 linkgit:git-status[1]
452 linkgit:git-rm[1]
453 linkgit:git-reset[1]
454 linkgit:git-mv[1]
455 linkgit:git-commit[1]
456 linkgit:git-update-index[1]
457
458 GIT
459 ---
460 Part of the linkgit:git[1] suite