LinuxCommandLibrary

git-add

Stage changes for commit

TLDR

Add a file to the index

$ git add [path/to/file]
copy

Add all files (tracked and untracked)
$ git add [[-A|--all]]
copy

Add all files recursively starting from the current folder
$ git add .
copy

Only add already tracked files
$ git add [[-u|--update]]
copy

Also add ignored files
$ git add [[-f|--force]]
copy

Interactively stage parts of files
$ git add [[-p|--patch]]
copy

Interactively stage parts of a given file
$ git add [[-p|--patch]] [path/to/file]
copy

Interactively stage a file
$ git add [[-i|--interactive]]
copy

SYNOPSIS

git add [] [--] [...]

PARAMETERS

-n, --dry-run
    Don't actually add the file(s), just show if they exist and/or will be ignored.

-v, --verbose
    Be verbose.

-f, --force
    Allow adding otherwise ignored files.

-i, --interactive
    Add modified contents interactively.

-p, --patch
    Add modified contents interactively.

-e, --edit
    Open the diff in an editor and let the user edit it before adding it.

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

--all, --no-ignore-removal
    Update the index not only where the working tree has a file matching but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.

--intent-to-add
    Record only the fact that the path will be added later. An entry for the path is placed in the index, but the file contents are not copied. This is useful for, among other things, showing the intent to add a file which you know will appear later, but do not need to include in this commit.

--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, do not abort the operation, but continue adding the others. The command shall still exit with a non-zero status.

--ignore-missing
    This option can only be used together with --dry-run. By using this option the command will not error out if do not exist.

--chmod=(+|-)x
    Override the executable bit of the added files.

--pathspec-from-file
    Pathspec is passed in instead of commandline args. If is exactly -, pathspec is read from standard input.

--pathspec-file-nul
    Only meaningful when --pathspec-from-file is used. Pathspec elements are separated with a NUL character and all other characters are taken literally (including newlines and quotes).

DESCRIPTION

The git add command is used to stage changes for the next commit. It adds the specified files or directories to the staging area (also known as the index). This command doesn't commit the changes to the repository; it only prepares them to be committed. Before you can commit changes, you must use git add to tell Git which changes you want to include in the next commit.

You can specify individual files, directories, or use wildcards to add multiple files at once. The command also intelligently handles new files and modifications to existing files. When you add a directory, Git recursively adds all files and subdirectories within that directory. git add . is a common shortcut to stage all changes in the current directory and its subdirectories. The changes are not permanently saved until you run git commit.

CAVEATS

Changes are only staged, not committed. You still need to run git commit to save them permanently.

IGNORING FILES

Files listed in the .gitignore file are typically excluded from being added unless explicitly specified using the -f or --force option.

INTERACTIVE ADDING

The -i and -p options provide interactive ways to review and stage changes, allowing you to selectively add parts of files to the staging area.

SEE ALSO

git commit(1), git reset(1), git rm(1), git status(1)

Copied to clipboard