NAME

git-add - Add file contents to the index

SYNOPSIS

  1. git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
  2. [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
  3. [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
  4. [--chmod=(+|-)x] [--] [<pathspec>…​]

DESCRIPTION

This command updates the index using the current content found inthe working tree, to prepare the content staged for the next commit.It typically adds the current content of existing paths as a whole,but with some options it can also be used to add content withonly part of the changes made to the working tree files applied, orremove paths that do not exist in the working tree anymore.

The "index" holds a snapshot of the content of the working tree, and itis this snapshot that is taken as the contents of the next commit. Thusafter making any changes to the working tree, and before runningthe commit command, you must use the add command to add any new ormodified files to the index.

This command can be performed multiple times before a commit. It onlyadds the content of the specified file(s) at the time the add command isrun; if you want subsequent changes included in the next commit, thenyou must run git add again to add the new content to the index.

The git status command can be used to obtain a summary of whichfiles have changes that are staged for the next commit.

The git add command will not add ignored files by default. If anyignored files were explicitly specified on the command line, git addwill fail with a list of ignored files. Ignored files reached bydirectory recursion or filename globbing performed by Git (quote yourglobs before the shell) will be silently ignored. The git add command canbe used to add ignored files with the -f (force) option.

Please see git-commit[1] for alternative ways to add content to acommit.

OPTIONS

  • …​
  • Files to add content from. Fileglobs (e.g. *.c) canbe given to add all matching files. Also aleading directory name (e.g. dir to add dir/file1and dir/file2) can be given to update the index tomatch the current state of the directory as a whole (e.g.specifying dir will record not just a file dir/file1modified in the working tree, a file dir/file2 added tothe working tree, but also a file dir/file3 removed fromthe working tree). Note that older versions of Git usedto ignore removed files; use —no-all option if you wantto add modified or new files but ignore removed ones.

For more details about the syntax, see the pathspec entryin gitglossary[7].

  • -n
  • —dry-run
  • Don’t actually add the file(s), just show if they exist and/or willbe ignored.

  • -v

  • —verbose
  • Be verbose.

  • -f

  • —force
  • Allow adding otherwise ignored files.

  • -i

  • —interactive
  • Add modified contents in the working tree interactively tothe index. Optional path arguments may be supplied to limitoperation to a subset of the working tree. See “Interactivemode” for details.

  • -p

  • —patch
  • Interactively choose hunks of patch between the index and thework tree and add them to the index. This gives the user a chanceto review the difference before adding modified contents to theindex.

This effectively runs add —interactive, but bypasses theinitial command menu and directly jumps to the patch subcommand.See “Interactive mode” for details.

  • -e
  • —edit
  • Open the diff vs. the index in an editor and let the useredit it. After the editor was closed, adjust the hunk headersand apply the patch to the index.

The intent of this option is to pick and choose lines of the patch toapply, or even to modify the contents of lines to be staged. This can bequicker and more flexible than using the interactive hunk selector.However, it is easy to confuse oneself and create a patch that does notapply to the index. See EDITING PATCHES below.

  • -u
  • —update
  • Update the index just where it already has an entry matching. This removes as well as modifies index entries tomatch the working tree, but adds no new files.

If no is given when -u option is used, alltracked files in the entire working tree are updated (old versionsof Git used to limit the update to the current directory and itssubdirectories).

  • -A
  • —all
  • —no-ignore-removal
  • Update the index not only where the working tree has a filematching but also where the index already has anentry. This adds, modifies, and removes index entries tomatch the working tree.

If no is given when -A option is used, allfiles in the entire working tree are updated (old versionsof Git used to limit the update to the current directory and itssubdirectories).

  • —no-all
  • —ignore-removal
  • Update the index by adding new files that are unknown to theindex and files modified in the working tree, but ignorefiles that have been removed from the working tree. Thisoption is a no-op when no is used.

This option is primarily to help users who are used to olderversions of Git, whose "git add …​" was a synonymfor "git add —no-all …​", i.e. ignored removed files.

  • -N
  • —intent-to-add
  • Record only the fact that the path will be added later. An entryfor the path is placed in the index with no content. This isuseful for, among other things, showing the unstaged content ofsuch files with git diff and committing them with git commit-a.

  • —refresh

  • Don’t add the file(s), but only refresh their stat()information in the index.

  • —ignore-errors

  • If some files could not be added because of errors indexingthem, do not abort the operation, but continue adding theothers. The command shall still exit with non-zero status.The configuration variable add.ignoreErrors can be set totrue to make this the default behaviour.

  • —ignore-missing

  • This option can only be used together with —dry-run. By usingthis option the user can check if any of the given files wouldbe ignored, no matter if they are already present in the worktree or not.

  • —no-warn-embedded-repo

  • By default, git add will warn when adding an embeddedrepository to the index without using git submodule add tocreate an entry in .gitmodules. This option will suppress thewarning (e.g., if you are manually performing operations onsubmodules).

  • —renormalize

  • Apply the "clean" process freshly to all tracked files toforcibly add them again to the index. This is useful afterchanging core.autocrlf configuration or the text attributein order to correct files added with wrong CRLF/LF line endings.This option implies -u.

  • —chmod=(+|-)x

  • Override the executable bit of the added files. The executablebit is only changed in the index, the files on disk are leftunchanged.

  • This option can be used to separate command-line options fromthe list of files, (useful when filenames might be mistakenfor command-line options).

EXAMPLES

  • Adds content from all *.txt files under Documentation directoryand its subdirectories:
  1. $ git add Documentation/\*.txt

Note that the asterisk * is quoted from the shell in thisexample; this lets the command include the files fromsubdirectories of Documentation/ directory.

  • Considers adding content from all git-*.sh scripts:
  1. $ git add git-*.sh

Because this example lets the shell expand the asterisk (i.e. you arelisting the files explicitly), it does not considersubdir/git-foo.sh.

INTERACTIVE MODE

When the command enters the interactive mode, it shows theoutput of the status subcommand, and then goes into itsinteractive command loop.

The command loop shows the list of subcommands available, andgives a prompt "What now> ". In general, when the prompt endswith a single >, you can pick only one of the choices givenand type return, like this:

  1. *** Commands ***
  2. 1: status 2: update 3: revert 4: add untracked
  3. 5: patch 6: diff 7: quit 8: help
  4. What now> 1

You also could say s or sta or status above as long as thechoice is unique.

The main command loop has 6 subcommands (plus help and quit).

  • status
  • This shows the change between HEAD and index (i.e. what will becommitted if you say git commit), and between index andworking tree files (i.e. what you could stage further beforegit commit using git add) for each path. A sample outputlooks like this:
  1. staged unstaged path
  2. 1: binary nothing foo.png
  3. 2: +403/-35 +1/-1 git-add--interactive.perl

It shows that foo.png has differences from HEAD (but that isbinary so line count cannot be shown) and there is nodifference between indexed copy and the working treeversion (if the working tree version were also different,binary would have been shown in place of nothing). Theother file, git-add{litdd}interactive.perl, has 403 lines addedand 35 lines deleted if you commit what is in the index, butworking tree file has further modifications (one addition andone deletion).

  • update
  • This shows the status information and issues an "Update>>"prompt. When the prompt ends with double >>, you canmake more than one selection, concatenated with whitespace orcomma. Also you can say ranges. E.g. "2-5 7,9" to choose2,3,4,5,7,9 from the list. If the second number in a range isomitted, all remaining patches are taken. E.g. "7-" to choose7,8,9 from the list. You can say * to choose everything.

What you chose are then highlighted with *,like this:

  1. staged unstaged path
  2. 1: binary nothing foo.png
  3. * 2: +403/-35 +1/-1 git-add--interactive.perl

To remove selection, prefix the input with -like this:

  1. Update>> -2

After making the selection, answer with an empty line to stage thecontents of working tree files for selected paths in the index.

  • revert
  • This has a very similar UI to update, and the stagedinformation for selected paths are reverted to that of theHEAD version. Reverting new paths makes them untracked.

  • add untracked

  • This has a very similar UI to update andrevert, and lets you add untracked paths to the index.

  • patch

  • This lets you choose one path out of a status like selection.After choosing the path, it presents the diff between the indexand the working tree file and asks you if you want to stagethe change of each hunk. You can select one of the followingoptions and type return:
  1. y - stage this hunk
  2. n - do not stage this hunk
  3. q - quit; do not stage this hunk or any of the remaining ones
  4. a - stage this hunk and all later hunks in the file
  5. d - do not stage this hunk or any of the later hunks in the file
  6. g - select a hunk to go to
  7. / - search for a hunk matching the given regex
  8. j - leave this hunk undecided, see next undecided hunk
  9. J - leave this hunk undecided, see next hunk
  10. k - leave this hunk undecided, see previous undecided hunk
  11. K - leave this hunk undecided, see previous hunk
  12. s - split the current hunk into smaller hunks
  13. e - manually edit the current hunk
  14. ? - print help

After deciding the fate for all hunks, if there is any hunkthat was chosen, the index is updated with the selected hunks.

You can omit having to type return here, by setting the configurationvariable interactive.singleKey to true.

  • diff
  • This lets you review what will be committed (i.e. betweenHEAD and index).

EDITING PATCHES

Invoking git add -e or selecting e from the interactive hunkselector will open a patch in your editor; after the editor exits, theresult is applied to the index. You are free to make arbitrary changesto the patch, but note that some changes may have confusing results, oreven result in a patch that cannot be applied. If you want to abort theoperation entirely (i.e., stage nothing new in the index), simply deleteall lines of the patch. The list below describes some common things youmay see in a patch, and which editing operations make sense on them.

  • added content
  • Added content is represented by lines beginning with "+". You canprevent staging any addition lines by deleting them.

  • removed content

  • Removed content is represented by lines beginning with "-". You canprevent staging their removal by converting the "-" to a " " (space).

  • modified content

  • Modified content is represented by "-" lines (removing the old content)followed by "+" lines (adding the replacement content). You canprevent staging the modification by converting "-" lines to " ", andremoving "+" lines. Beware that modifying only half of the pair islikely to introduce confusing changes to the index.

There are also more complex operations that can be performed. But bewarethat because the patch is applied only to the index and not the workingtree, the working tree will appear to "undo" the change in the index.For example, introducing a new line into the index that is in neitherthe HEAD nor the working tree will stage the new line for commit, butthe line will appear to be reverted in the working tree.

Avoid using these constructs, or do so with extreme caution.

  • removing untouched content
  • Content which does not differ between the index and working tree may beshown on context lines, beginning with a " " (space). You can stagecontext lines for removal by converting the space to a "-". Theresulting working tree file will appear to re-add the content.

  • modifying existing content

  • One can also modify context lines by staging them for removal (byconverting " " to "-") and adding a "+" line with the new content.Similarly, one can modify "+" lines for existing additions ormodifications. In all cases, the new modification will appear revertedin the working tree.

  • new content

  • You may also add new content that does not exist in the patch; simplyadd new lines, each starting with "+". The addition will appearreverted in the working tree.

There are also several operations which should be avoided entirely, asthey will make the patch impossible to apply:

  • adding context (" ") or removal ("-") lines

  • deleting context or removal lines

  • modifying the contents of context or removal lines

SEE ALSO

git-status[1]git-rm[1]git-reset[1]git-mv[1]git-commit[1]git-update-index[1]

GIT

Part of the git[1] suite