1GIT-ADD(1) Git Manual GIT-ADD(1)
2
3
4
6 git-add - Add file contents to the index
7
9 git add [-n] [-v] [--force | -f] [--interactive | -i] [--patch | -p]
10 [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
11 [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing]
12 [--] [<pathspec>...]
13
14
16 This command updates the index using the current content found in the
17 working tree, to prepare the content staged for the next commit. It
18 typically adds the current content of existing paths as a whole, but
19 with some options it can also be used to add content with only part of
20 the changes made to the working tree files applied, or remove paths
21 that do not exist in the working tree anymore.
22
23 The "index" holds a snapshot of the content of the working tree, and it
24 is this snapshot that is taken as the contents of the next commit. Thus
25 after making any changes to the working directory, and before running
26 the commit command, you must use the add command to add any new or
27 modified files to the index.
28
29 This command can be performed multiple times before a commit. It only
30 adds the content of the specified file(s) at the time the add command
31 is run; if you want subsequent changes included in the next commit,
32 then you must run git add again to add the new content to the index.
33
34 The git status command can be used to obtain a summary of which files
35 have changes that are staged for the next commit.
36
37 The git add command will not add ignored files by default. If any
38 ignored files were explicitly specified on the command line, git add
39 will fail with a list of ignored files. Ignored files reached by
40 directory recursion or filename globbing performed by Git (quote your
41 globs before the shell) will be silently ignored. The git add command
42 can be used to add ignored files with the -f (force) option.
43
44 Please see git-commit(1) for alternative ways to add content to a
45 commit.
46
48 <pathspec>...
49 Files to add content from. Fileglobs (e.g. *.c) can be given to
50 add all matching files. Also a leading directory name (e.g. dir to
51 add dir/file1 and dir/file2) can be given to add all files in the
52 directory, recursively.
53
54 -n, --dry-run
55 Don’t actually add the file(s), just show if they exist and/or will
56 be ignored.
57
58 -v, --verbose
59 Be verbose.
60
61 -f, --force
62 Allow adding otherwise ignored files.
63
64 -i, --interactive
65 Add modified contents in the working tree interactively to the
66 index. Optional path arguments may be supplied to limit operation
67 to a subset of the working tree. See “Interactive mode” for
68 details.
69
70 -p, --patch
71 Interactively choose hunks of patch between the index and the work
72 tree and add them to the index. This gives the user a chance to
73 review the difference before adding modified contents to the index.
74
75 This effectively runs add --interactive, but bypasses the initial
76 command menu and directly jumps to the patch subcommand. See
77 “Interactive mode” for details.
78
79 -e, --edit
80 Open the diff vs. the index in an editor and let the user edit it.
81 After the editor was closed, adjust the hunk headers and apply the
82 patch to the index.
83
84 The intent of this option is to pick and choose lines of the patch
85 to apply, or even to modify the contents of lines to be staged.
86 This can be quicker and more flexible than using the interactive
87 hunk selector. However, it is easy to confuse oneself and create a
88 patch that does not apply to the index. See EDITING PATCHES below.
89
90 -u, --update
91 Update the index just where it already has an entry matching
92 <pathspec>. This removes as well as modifies index entries to match
93 the working tree, but adds no new files.
94
95 If no <pathspec> is given, the current version of Git defaults to
96 "."; in other words, update all tracked files in the current
97 directory and its subdirectories. This default will change in a
98 future version of Git, hence the form without <pathspec> should not
99 be used.
100
101 -A, --all, --no-ignore-removal
102 Update the index not only where the working tree has a file
103 matching <pathspec> but also where the index already has an entry.
104 This adds, modifies, and removes index entries to match the working
105 tree.
106
107 If no <pathspec> is given, the current version of Git defaults to
108 "."; in other words, update all files in the current directory and
109 its subdirectories. This default will change in a future version of
110 Git, hence the form without <pathspec> should not be used.
111
112 --no-all, --ignore-removal
113 Update the index by adding new files that are unknown to the index
114 and files modified in the working tree, but ignore files that have
115 been removed from the working tree. This option is a no-op when no
116 <pathspec> is used.
117
118 This option is primarily to help the current users of Git, whose
119 "git add <pathspec>..." ignores removed files. In future versions
120 of Git, "git add <pathspec>..." will be a synonym to "git add -A
121 <pathspec>..." and "git add --ignore-removal <pathspec>..." will
122 behave like today’s "git add <pathspec>...", ignoring removed
123 files.
124
125 -N, --intent-to-add
126 Record only the fact that the path will be added later. An entry
127 for the path is placed in the index with no content. This is useful
128 for, among other things, showing the unstaged content of such files
129 with git diff and committing them with git commit -a.
130
131 --refresh
132 Don’t add the file(s), but only refresh their stat() information in
133 the index.
134
135 --ignore-errors
136 If some files could not be added because of errors indexing them,
137 do not abort the operation, but continue adding the others. The
138 command shall still exit with non-zero status. The configuration
139 variable add.ignoreErrors can be set to true to make this the
140 default behaviour.
141
142 --ignore-missing
143 This option can only be used together with --dry-run. By using this
144 option the user can check if any of the given files would be
145 ignored, no matter if they are already present in the work tree or
146 not.
147
148 --
149 This option can be used to separate command-line options from the
150 list of files, (useful when filenames might be mistaken for
151 command-line options).
152
154 The optional configuration variable core.excludesfile indicates a path
155 to a file containing patterns of file names to exclude from git-add,
156 similar to $GIT_DIR/info/exclude. Patterns in the exclude file are used
157 in addition to those in info/exclude. See gitignore(5).
158
160 · Adds content from all *.txt files under Documentation directory and
161 its subdirectories:
162
163 $ git add Documentation/\*.txt
164
165 Note that the asterisk * is quoted from the shell in this example;
166 this lets the command include the files from subdirectories of
167 Documentation/ directory.
168
169 · Considers adding content from all git-*.sh scripts:
170
171 $ git add git-*.sh
172
173 Because this example lets the shell expand the asterisk (i.e. you
174 are listing the files explicitly), it does not consider
175 subdir/git-foo.sh.
176
178 When the command enters the interactive mode, it shows the output of
179 the status subcommand, and then goes into its interactive command loop.
180
181 The command loop shows the list of subcommands available, and gives a
182 prompt "What now> ". In general, when the prompt ends with a single >,
183 you can pick only one of the choices given and type return, like this:
184
185 *** Commands ***
186 1: status 2: update 3: revert 4: add untracked
187 5: patch 6: diff 7: quit 8: help
188 What now> 1
189
190
191 You also could say s or sta or status above as long as the choice is
192 unique.
193
194 The main command loop has 6 subcommands (plus help and quit).
195
196 status
197 This shows the change between HEAD and index (i.e. what will be
198 committed if you say git commit), and between index and working
199 tree files (i.e. what you could stage further before git commit
200 using git add) for each path. A sample output looks like this:
201
202 staged unstaged path
203 1: binary nothing foo.png
204 2: +403/-35 +1/-1 git-add--interactive.perl
205
206 It shows that foo.png has differences from HEAD (but that is binary
207 so line count cannot be shown) and there is no difference between
208 indexed copy and the working tree version (if the working tree
209 version were also different, binary would have been shown in place
210 of nothing). The other file, git-add--interactive.perl, has 403
211 lines added and 35 lines deleted if you commit what is in the
212 index, but working tree file has further modifications (one
213 addition and one deletion).
214
215 update
216 This shows the status information and issues an "Update>>" prompt.
217 When the prompt ends with double >>, you can make more than one
218 selection, concatenated with whitespace or comma. Also you can say
219 ranges. E.g. "2-5 7,9" to choose 2,3,4,5,7,9 from the list. If the
220 second number in a range is omitted, all remaining patches are
221 taken. E.g. "7-" to choose 7,8,9 from the list. You can say * to
222 choose everything.
223
224 What you chose are then highlighted with *, like this:
225
226 staged unstaged path
227 1: binary nothing foo.png
228 * 2: +403/-35 +1/-1 git-add--interactive.perl
229
230 To remove selection, prefix the input with - like this:
231
232 Update>> -2
233
234 After making the selection, answer with an empty line to stage the
235 contents of working tree files for selected paths in the index.
236
237 revert
238 This has a very similar UI to update, and the staged information
239 for selected paths are reverted to that of the HEAD version.
240 Reverting new paths makes them untracked.
241
242 add untracked
243 This has a very similar UI to update and revert, and lets you add
244 untracked paths to the index.
245
246 patch
247 This lets you choose one path out of a status like selection. After
248 choosing the path, it presents the diff between the index and the
249 working tree file and asks you if you want to stage the change of
250 each hunk. You can select one of the following options and type
251 return:
252
253 y - stage this hunk
254 n - do not stage this hunk
255 q - quit; do not stage this hunk nor any of the remaining ones
256 a - stage this hunk and all later hunks in the file
257 d - do not stage this hunk nor any of the later hunks in the file
258 g - select a hunk to go to
259 / - search for a hunk matching the given regex
260 j - leave this hunk undecided, see next undecided hunk
261 J - leave this hunk undecided, see next hunk
262 k - leave this hunk undecided, see previous undecided hunk
263 K - leave this hunk undecided, see previous hunk
264 s - split the current hunk into smaller hunks
265 e - manually edit the current hunk
266 ? - print help
267
268 After deciding the fate for all hunks, if there is any hunk that
269 was chosen, the index is updated with the selected hunks.
270
271 You can omit having to type return here, by setting the
272 configuration variable interactive.singlekey to true.
273
274 diff
275 This lets you review what will be committed (i.e. between HEAD and
276 index).
277
279 Invoking git add -e or selecting e from the interactive hunk selector
280 will open a patch in your editor; after the editor exits, the result is
281 applied to the index. You are free to make arbitrary changes to the
282 patch, but note that some changes may have confusing results, or even
283 result in a patch that cannot be applied. If you want to abort the
284 operation entirely (i.e., stage nothing new in the index), simply
285 delete all lines of the patch. The list below describes some common
286 things you may see in a patch, and which editing operations make sense
287 on them.
288
289 added content
290 Added content is represented by lines beginning with "+". You can
291 prevent staging any addition lines by deleting them.
292
293 removed content
294 Removed content is represented by lines beginning with "-". You can
295 prevent staging their removal by converting the "-" to a " "
296 (space).
297
298 modified content
299 Modified content is represented by "-" lines (removing the old
300 content) followed by "+" lines (adding the replacement content).
301 You can prevent staging the modification by converting "-" lines to
302 " ", and removing "+" lines. Beware that modifying only half of the
303 pair is likely to introduce confusing changes to the index.
304
305 There are also more complex operations that can be performed. But
306 beware that because the patch is applied only to the index and not the
307 working tree, the working tree will appear to "undo" the change in the
308 index. For example, introducing a new line into the index that is in
309 neither the HEAD nor the working tree will stage the new line for
310 commit, but the line will appear to be reverted in the working tree.
311
312 Avoid using these constructs, or do so with extreme caution.
313
314 removing untouched content
315 Content which does not differ between the index and working tree
316 may be shown on context lines, beginning with a " " (space). You
317 can stage context lines for removal by converting the space to a
318 "-". The resulting working tree file will appear to re-add the
319 content.
320
321 modifying existing content
322 One can also modify context lines by staging them for removal (by
323 converting " " to "-") and adding a "+" line with the new content.
324 Similarly, one can modify "+" lines for existing additions or
325 modifications. In all cases, the new modification will appear
326 reverted in the working tree.
327
328 new content
329 You may also add new content that does not exist in the patch;
330 simply add new lines, each starting with "+". The addition will
331 appear reverted in the working tree.
332
333 There are also several operations which should be avoided entirely, as
334 they will make the patch impossible to apply:
335
336 · adding context (" ") or removal ("-") lines
337
338 · deleting context or removal lines
339
340 · modifying the contents of context or removal lines
341
343 git-status(1) git-rm(1) git-reset(1) git-mv(1) git-commit(1) git-
344 update-index(1)
345
347 Part of the git(1) suite
348
349
350
351Git 1.8.3.1 11/19/2018 GIT-ADD(1)