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