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.
Initial Example
To add a file to the staging area, use:
git add filename
ADD Options
Option | Description |
---|---|
<pathspec>... | Specify the files to add content from. Use file patterns like *.c or directory names to update the index accordingly. |
-n, --dry-run | Show which files would be added without actually adding them. |
-v, --verbose | Display detailed information about the files being added. |
-f, --force | Add files that are otherwise ignored by .gitignore . |
--sparse | Update index entries outside of the sparse-checkout cone. |
-i, --interactive | Add modified contents interactively, allowing you to choose what to add. |
-p, --patch | Interactively choose hunks of patch to add to the index. |
-e, --edit | Open the diff in an editor to edit and apply changes to the index. |
-u, --update | Update the index for files that are already tracked, matching the working tree. |
-A, --all, --no-ignore-removal | Update the index to match the working tree, adding, modifying, and removing files. |
--no-all, --ignore-removal | Add new files and modified files, but ignore removed files. |
-N, --intent-to-add | Record only the fact that the path will be added later, showing unstaged content with git diff . |
--refresh | Refresh the index without actually adding the files. |
--ignore-errors | Continue adding files even if some could not be added due to errors, still exiting with a non-zero status. |
--ignore-missing | With --dry-run , check if the given files would be ignored, even if they are not present in the working tree. |
--no-warn-embedded-repo | Suppress warnings when adding an embedded repository without using git submodule add . |
--renormalize | Reapply the “clean” process to all tracked files, useful after changing configuration that affects line endings. |
--chmod=(+|-)x | Change 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-nul | Separate 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/