Git add

The git add command is used to add changes in the working directory to the staging area. This command tells Git that you want to include updates to a particular file or directory in the next commit.



Tutorials dojo strip

Initial Example

To add a file to the staging area, use:

git add filename




ADD Options

OptionDescription
<pathspec>...Specify the files to add content from. Use file patterns like *.c or directory names to update the index accordingly.
-n, --dry-runShow which files would be added without actually adding them.
-v, --verboseDisplay detailed information about the files being added.
-f, --forceAdd files that are otherwise ignored by .gitignore.
--sparseUpdate index entries outside of the sparse-checkout cone.
-i, --interactiveAdd modified contents interactively, allowing you to choose what to add.
-p, --patchInteractively choose hunks of patch to add to the index.
-e, --editOpen the diff in an editor to edit and apply changes to the index.
-u, --updateUpdate the index for files that are already tracked, matching the working tree.
-A, --all, --no-ignore-removalUpdate the index to match the working tree, adding, modifying, and removing files.
--no-all, --ignore-removalAdd new files and modified files, but ignore removed files.
-N, --intent-to-addRecord only the fact that the path will be added later, showing unstaged content with git diff.
--refreshRefresh the index without actually adding the files.
--ignore-errorsContinue adding files even if some could not be added due to errors, still exiting with a non-zero status.
--ignore-missingWith --dry-run, check if the given files would be ignored, even if they are not present in the working tree.
--no-warn-embedded-repoSuppress warnings when adding an embedded repository without using git submodule add.
--renormalizeReapply the “clean” process to all tracked files, useful after changing configuration that affects line endings.
--chmod=(+|-)xChange the executable bit of added files in the index, without modifying the files on disk.
--pathspec-from-file=<file>Read the pathspec from the specified file instead of command-line arguments.
--pathspec-file-nulSeparate pathspec elements with a NUL character, used with --pathspec-from-file.
--Separate command-line options from the list of files, useful when filenames might be mistaken for options.




Examples

1. Adding a Single File

If you made changes to example.txt and want to include it in the next commit, you would use:

git add example.txt

2. Adding Multiple Files

You can add multiple files by specifying each one:

git add file1.txt file2.txt

3. Adding All Changes

To add all the changes in the current directory and its subdirectories, use:

git add .

4. Adding Files Matching a Pattern

To add all files matching a specific pattern, such as all .txt files, use:

git add *.txt

5. Adding a Directory

To add an entire directory and its contents, use:

git add directory/
Scroll to Top