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] [--all | [--update | -u]] [--intent-to-add | -N]
11 [--refresh] [--ignore-errors] [--ignore-missing] [--]
12 [<filepattern>...]
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 <filepattern>...
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 Only match <filepattern> against already tracked files in the index
92 rather than the working tree. That means that it will never stage
93 new files, but that it will stage modified new contents of tracked
94 files and that it will remove files from the index if the
95 corresponding files in the working tree have been removed.
96
97 If no <filepattern> is given, default to "."; in other words,
98 update all tracked files in the current directory and its
99 subdirectories.
100
101 -A, --all
102 Like -u, but match <filepattern> against files in the working tree
103 in addition to the index. That means that it will find new files as
104 well as staging modified content and removing files that are no
105 longer in the working tree.
106
107 -N, --intent-to-add
108 Record only the fact that the path will be added later. An entry
109 for the path is placed in the index with no content. This is useful
110 for, among other things, showing the unstaged content of such files
111 with git diff and committing them with git commit -a.
112
113 --refresh
114 Don’t add the file(s), but only refresh their stat() information in
115 the index.
116
117 --ignore-errors
118 If some files could not be added because of errors indexing them,
119 do not abort the operation, but continue adding the others. The
120 command shall still exit with non-zero status.
121
122 --ignore-missing
123 This option can only be used together with --dry-run. By using this
124 option the user can check if any of the given files would be
125 ignored, no matter if they are already present in the work tree or
126 not.
127
128 --
129 This option can be used to separate command-line options from the
130 list of files, (useful when filenames might be mistaken for
131 command-line options).
132
134 The optional configuration variable core.excludesfile indicates a path
135 to a file containing patterns of file names to exclude from git-add,
136 similar to $GIT_DIR/info/exclude. Patterns in the exclude file are used
137 in addition to those in info/exclude. See gitrepository-layout(5).
138
140 · Adds content from all *.txt files under Documentation directory and
141 its subdirectories:
142
143 $ git add Documentation/\*.txt
144
145 Note that the asterisk * is quoted from the shell in this example;
146 this lets the command include the files from subdirectories of
147 Documentation/ directory.
148
149 · Considers adding content from all git-*.sh scripts:
150
151 $ git add git-*.sh
152
153 Because this example lets the shell expand the asterisk (i.e. you
154 are listing the files explicitly), it does not consider
155 subdir/git-foo.sh.
156
158 When the command enters the interactive mode, it shows the output of
159 the status subcommand, and then goes into its interactive command loop.
160
161 The command loop shows the list of subcommands available, and gives a
162 prompt "What now> ". In general, when the prompt ends with a single >,
163 you can pick only one of the choices given and type return, like this:
164
165 *** Commands ***
166 1: status 2: update 3: revert 4: add untracked
167 5: patch 6: diff 7: quit 8: help
168 What now> 1
169
170
171 You also could say s or sta or status above as long as the choice is
172 unique.
173
174 The main command loop has 6 subcommands (plus help and quit).
175
176 status
177 This shows the change between HEAD and index (i.e. what will be
178 committed if you say git commit), and between index and working
179 tree files (i.e. what you could stage further before git commit
180 using git add) for each path. A sample output looks like this:
181
182 staged unstaged path
183 1: binary nothing foo.png
184 2: +403/-35 +1/-1 git-add--interactive.perl
185
186 It shows that foo.png has differences from HEAD (but that is binary
187 so line count cannot be shown) and there is no difference between
188 indexed copy and the working tree version (if the working tree
189 version were also different, binary would have been shown in place
190 of nothing). The other file, git-add--interactive.perl, has 403
191 lines added and 35 lines deleted if you commit what is in the
192 index, but working tree file has further modifications (one
193 addition and one deletion).
194
195 update
196 This shows the status information and issues an "Update>>" prompt.
197 When the prompt ends with double >>, you can make more than one
198 selection, concatenated with whitespace or comma. Also you can say
199 ranges. E.g. "2-5 7,9" to choose 2,3,4,5,7,9 from the list. If the
200 second number in a range is omitted, all remaining patches are
201 taken. E.g. "7-" to choose 7,8,9 from the list. You can say * to
202 choose everything.
203
204 What you chose are then highlighted with *, like this:
205
206 staged unstaged path
207 1: binary nothing foo.png
208 * 2: +403/-35 +1/-1 git-add--interactive.perl
209
210 To remove selection, prefix the input with - like this:
211
212 Update>> -2
213
214 After making the selection, answer with an empty line to stage the
215 contents of working tree files for selected paths in the index.
216
217 revert
218 This has a very similar UI to update, and the staged information
219 for selected paths are reverted to that of the HEAD version.
220 Reverting new paths makes them untracked.
221
222 add untracked
223 This has a very similar UI to update and revert, and lets you add
224 untracked paths to the index.
225
226 patch
227 This lets you choose one path out of a status like selection. After
228 choosing the path, it presents the diff between the index and the
229 working tree file and asks you if you want to stage the change of
230 each hunk. You can say:
231
232 y - stage this hunk
233 n - do not stage this hunk
234 q - quit; do not stage this hunk nor any of the remaining ones
235 a - stage this hunk and all later hunks in the file
236 d - do not stage this hunk nor any of the later hunks in the file
237 g - select a hunk to go to
238 / - search for a hunk matching the given regex
239 j - leave this hunk undecided, see next undecided hunk
240 J - leave this hunk undecided, see next hunk
241 k - leave this hunk undecided, see previous undecided hunk
242 K - leave this hunk undecided, see previous hunk
243 s - split the current hunk into smaller hunks
244 e - manually edit the current hunk
245 ? - print help
246
247 After deciding the fate for all hunks, if there is any hunk that
248 was chosen, the index is updated with the selected hunks.
249
250 diff
251 This lets you review what will be committed (i.e. between HEAD and
252 index).
253
255 Invoking git add -e or selecting e from the interactive hunk selector
256 will open a patch in your editor; after the editor exits, the result is
257 applied to the index. You are free to make arbitrary changes to the
258 patch, but note that some changes may have confusing results, or even
259 result in a patch that cannot be applied. If you want to abort the
260 operation entirely (i.e., stage nothing new in the index), simply
261 delete all lines of the patch. The list below describes some common
262 things you may see in a patch, and which editing operations make sense
263 on them.
264
265 added content
266 Added content is represented by lines beginning with "+". You can
267 prevent staging any addition lines by deleting them.
268
269 removed content
270 Removed content is represented by lines beginning with "-". You can
271 prevent staging their removal by converting the "-" to a " "
272 (space).
273
274 modified content
275 Modified content is represented by "-" lines (removing the old
276 content) followed by "+" lines (adding the replacement content).
277 You can prevent staging the modification by converting "-" lines to
278 " ", and removing "+" lines. Beware that modifying only half of the
279 pair is likely to introduce confusing changes to the index.
280
281 There are also more complex operations that can be performed. But
282 beware that because the patch is applied only to the index and not the
283 working tree, the working tree will appear to "undo" the change in the
284 index. For example, introducing a new line into the index that is in
285 neither the HEAD nor the working tree will stage the new line for
286 commit, but the line will appear to be reverted in the working tree.
287
288 Avoid using these constructs, or do so with extreme caution.
289
290 removing untouched content
291 Content which does not differ between the index and working tree
292 may be shown on context lines, beginning with a " " (space). You
293 can stage context lines for removal by converting the space to a
294 "-". The resulting working tree file will appear to re-add the
295 content.
296
297 modifying existing content
298 One can also modify context lines by staging them for removal (by
299 converting " " to "-") and adding a "+" line with the new content.
300 Similarly, one can modify "+" lines for existing additions or
301 modifications. In all cases, the new modification will appear
302 reverted in the working tree.
303
304 new content
305 You may also add new content that does not exist in the patch;
306 simply add new lines, each starting with "+". The addition will
307 appear reverted in the working tree.
308
309 There are also several operations which should be avoided entirely, as
310 they will make the patch impossible to apply:
311
312 · adding context (" ") or removal ("-") lines
313
314 · deleting context or removal lines
315
316 · modifying the contents of context or removal lines
317
319 git-status(1) git-rm(1) git-reset(1) git-mv(1) git-commit(1) git-
320 update-index(1)
321
323 Written by Linus Torvalds <torvalds@osdl.org[1]>
324
326 Documentation by Junio C Hamano and the git-list
327 <git@vger.kernel.org[2]>.
328
330 Part of the git(1) suite
331
333 1. torvalds@osdl.org
334 mailto:torvalds@osdl.org
335
336 2. git@vger.kernel.org
337 mailto:git@vger.kernel.org
338
339
340
341Git 1.7.4.4 04/11/2011 GIT-ADD(1)