| 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 | [--resolved] [--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 | `--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 | |
| 230 | EXAMPLES |
| 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 | + |
| 240 | Note that the asterisk ++*++ is quoted from the shell in this |
| 241 | example; this lets the command include the files from |
| 242 | subdirectories of `Documentation/` directory. |
| 243 | |
| 244 | * Considers adding content from all ++git-*.sh++ scripts: |
| 245 | + |
| 246 | ------------ |
| 247 | $ git add git-*.sh |
| 248 | ------------ |
| 249 | + |
| 250 | Because this example lets the shell expand the asterisk (i.e. you are |
| 251 | listing the files explicitly), it does not consider |
| 252 | `subdir/git-foo.sh`. |
| 253 | |
| 254 | INTERACTIVE MODE |
| 255 | ---------------- |
| 256 | When the command enters the interactive mode, it shows the |
| 257 | output of the 'status' subcommand, and then goes into its |
| 258 | interactive command loop. |
| 259 | |
| 260 | The command loop shows the list of subcommands available, and |
| 261 | gives a prompt "What now> ". In general, when the prompt ends |
| 262 | with a single '>', you can pick only one of the choices given |
| 263 | and 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 | |
| 272 | You also could say `s` or `sta` or `status` above as long as the |
| 273 | choice is unique. |
| 274 | |
| 275 | The main command loop has 6 subcommands (plus help and quit). |
| 276 | |
| 277 | status:: |
| 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 | + |
| 291 | It shows that `foo.png` has differences from `HEAD` (but that is |
| 292 | binary so line count cannot be shown) and there is no |
| 293 | difference between indexed copy and the working tree |
| 294 | version (if the working tree version were also different, |
| 295 | 'binary' would have been shown in place of 'nothing'). The |
| 296 | other file, `add-interactive.c`, has 403 lines added |
| 297 | and 35 lines deleted if you commit what is in the index, but |
| 298 | working tree file has further modifications (one addition and |
| 299 | one deletion). |
| 300 | |
| 301 | update:: |
| 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 | + |
| 311 | What you chose are then highlighted with '*', |
| 312 | like this: |
| 313 | + |
| 314 | ------------ |
| 315 | staged unstaged path |
| 316 | 1: binary nothing foo.png |
| 317 | * 2: +403/-35 +1/-1 add-interactive.c |
| 318 | ------------ |
| 319 | + |
| 320 | To remove selection, prefix the input with `-` |
| 321 | like this: |
| 322 | + |
| 323 | ------------ |
| 324 | Update>> -2 |
| 325 | ------------ |
| 326 | + |
| 327 | After making the selection, answer with an empty line to stage the |
| 328 | contents of working tree files for selected paths in the index. |
| 329 | |
| 330 | revert:: |
| 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 | |
| 336 | add untracked:: |
| 337 | |
| 338 | This has a very similar UI to 'update' and |
| 339 | 'revert', and lets you add untracked paths to the index. |
| 340 | |
| 341 | patch:: |
| 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 | + |
| 366 | After deciding the fate for all hunks, if there is any hunk |
| 367 | that was chosen, the index is updated with the selected hunks. |
| 368 | + |
| 369 | You can omit having to type return here, by setting the configuration |
| 370 | variable `interactive.singleKey` to `true`. |
| 371 | |
| 372 | diff:: |
| 373 | |
| 374 | This lets you review what will be committed (i.e. between |
| 375 | `HEAD` and index). |
| 376 | |
| 377 | |
| 378 | EDITING PATCHES |
| 379 | --------------- |
| 380 | |
| 381 | Invoking `git add -e` or selecting `e` from the interactive hunk |
| 382 | selector will open a patch in your editor; after the editor exits, the |
| 383 | result is applied to the index. You are free to make arbitrary changes |
| 384 | to the patch, but note that some changes may have confusing results, or |
| 385 | even result in a patch that cannot be applied. If you want to abort the |
| 386 | operation entirely (i.e., stage nothing new in the index), simply delete |
| 387 | all lines of the patch. The list below describes some common things you |
| 388 | may see in a patch, and which editing operations make sense on them. |
| 389 | |
| 390 | -- |
| 391 | added content:: |
| 392 | |
| 393 | Added content is represented by lines beginning with "{plus}". You can |
| 394 | prevent staging any addition lines by deleting them. |
| 395 | |
| 396 | removed content:: |
| 397 | |
| 398 | Removed content is represented by lines beginning with "-". You can |
| 399 | prevent staging their removal by converting the "-" to a " " (space). |
| 400 | |
| 401 | modified content:: |
| 402 | |
| 403 | Modified content is represented by "-" lines (removing the old content) |
| 404 | followed by "{plus}" lines (adding the replacement content). You can |
| 405 | prevent staging the modification by converting "-" lines to " ", and |
| 406 | removing "{plus}" lines. Beware that modifying only half of the pair is |
| 407 | likely to introduce confusing changes to the index. |
| 408 | -- |
| 409 | |
| 410 | There are also more complex operations that can be performed. But beware |
| 411 | that because the patch is applied only to the index and not the working |
| 412 | tree, the working tree will appear to "undo" the change in the index. |
| 413 | For example, introducing a new line into the index that is in neither |
| 414 | the `HEAD` nor the working tree will stage the new line for commit, but |
| 415 | the line will appear to be reverted in the working tree. |
| 416 | |
| 417 | Avoid using these constructs, or do so with extreme caution. |
| 418 | |
| 419 | -- |
| 420 | removing untouched content:: |
| 421 | |
| 422 | Content which does not differ between the index and working tree may be |
| 423 | shown on context lines, beginning with a " " (space). You can stage |
| 424 | context lines for removal by converting the space to a "-". The |
| 425 | resulting working tree file will appear to re-add the content. |
| 426 | |
| 427 | modifying existing content:: |
| 428 | |
| 429 | One can also modify context lines by staging them for removal (by |
| 430 | converting " " to "-") and adding a "{plus}" line with the new content. |
| 431 | Similarly, one can modify "{plus}" lines for existing additions or |
| 432 | modifications. In all cases, the new modification will appear reverted |
| 433 | in the working tree. |
| 434 | |
| 435 | new content:: |
| 436 | |
| 437 | You may also add new content that does not exist in the patch; simply |
| 438 | add new lines, each starting with "{plus}". The addition will appear |
| 439 | reverted in the working tree. |
| 440 | -- |
| 441 | |
| 442 | There are also several operations which should be avoided entirely, as |
| 443 | they will make the patch impossible to apply: |
| 444 | |
| 445 | * adding context (" ") or removal ("-") lines |
| 446 | * deleting context or removal lines |
| 447 | * modifying the contents of context or removal lines |
| 448 | |
| 449 | CONFIGURATION |
| 450 | ------------- |
| 451 | |
| 452 | include::includes/cmd-config-section-all.adoc[] |
| 453 | |
| 454 | :git-add: 1 |
| 455 | include::config/add.adoc[] |
| 456 | |
| 457 | SEE ALSO |
| 458 | -------- |
| 459 | linkgit:git-status[1] |
| 460 | linkgit:git-rm[1] |
| 461 | linkgit:git-reset[1] |
| 462 | linkgit:git-mv[1] |
| 463 | linkgit:git-commit[1] |
| 464 | linkgit:git-update-index[1] |
| 465 | |
| 466 | GIT |
| 467 | --- |
| 468 | Part of the linkgit:git[1] suite |