git add

Basic Snapshotting

Add file contents to the index (staging area)


The 'git add' command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. The index, also known as the staging area, is what you use to prepare the contents of the next commit. When you run 'git commit' without any other arguments, it will only commit staged changes. This command can be performed multiple times before a commit to add incremental changes.

Synopsis

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [--all | -A] [--update | -u] [--] [<pathspec>...]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (4)

Stage everything at once - all new files, modifications, and deletions in your entire project.

When neededStages everything, deletions included — check git status first so nothing unwanted slips in

Example

git add -A

Only stage changes to files Git already knows about. New untracked files will be ignored.

When neededStages changes to tracked files only — handy when new files are not ready yet

Example

git add -u

See exactly which files are being staged as the command runs. Prints each filename as it gets added.

When neededLists each file as it is staged; useful for large adds

Example

git add -v .

Preview what would be staged without actually staging anything. Great for double-checking before you commit.

RecommendedHarmless preview of what a broad add would stage

Example

git add -n .

Intermediate (5)

Interactively select which parts of a file to stage. Perfect for splitting large changes into focused commits.

RecommendedReview and stage exactly the hunks that belong in this commit

Example

git add -p

Open a full menu-driven interface to manage staging. Offers options to stage, unstage, and review changes interactively.

When neededMenu-driven staging; -p covers most needs more directly

Example

git add -i

Stage a file that's normally ignored by .gitignore. Use when you need to override ignore rules.

When neededStages ignored files — make sure the file really belongs in the repository

Example

git add -f dist/bundle.js

Record that a file will be added later, without staging its content. Useful for showing new files in 'git diff'.

When neededMakes new files visible to git diff and add -p before you stage them

Example

git add -N newfile.js

Suppress the warning when adding an embedded repository (a folder with its own .git) to the index. Use when you intentionally want to add a nested git repo.

Not recommendedUsually you want git submodule add instead of a bare embedded repo

Example

git add --no-warn-embedded-repo vendor/library

Advanced (6)

Open the diff in your editor before staging. You can manually edit which changes to include.

When neededHand-edit the diff before staging; easy to produce a patch that will not apply

Example

git add -e app.js

Don't add files, just refresh their stat() info in the index. Needs a pathspec. Useful after timestamp-only changes.

When neededOnly refreshes stat info in the index; git status already does this

Example

git add --refresh .

Continue adding files even if some fail due to errors. The command still exits with non-zero status.

When neededSkips files that cannot be added; the command still exits non-zero

Example

git add --ignore-errors .

Check if files would be ignored, even if they don't exist yet. Only works with --dry-run.

When neededOnly with --dry-run: checks ignore rules even for missing paths

Example

git add -n --ignore-missing *.tmp

Stage new and modified files, but ignore files removed from the working tree. Alias: --ignore-removal. It only has an effect together with a pathspec (e.g. git add --no-all .).

Not recommendedLegacy pre-2.0 behaviour that silently leaves deletions unstaged

Example

git add --no-all .

Generate the hunks shown by -p/-i with <n> lines of context instead of the default 3 (or diff.context). Requires Git 2.51 or newer — older versions reject it with "unknown switch `U'".

When neededMore context lines in add -p hunks — needs Git 2.51 or newer

Example

git add -p -U5 app.js

Expert (6)

Override the executable bit of added files. Use +x to make executable, -x to remove.

When neededSets the executable bit in the index — useful on Windows

Example

git add --chmod=+x script.sh

Reapply the 'clean' filter to all tracked files. Useful after changing line ending settings.

When neededRun once after changing .gitattributes line-ending rules

Example

git add --renormalize .

Allow updating index entries outside the sparse-checkout cone. For advanced sparse checkouts only.

When neededOnly for sparse-checkout repos editing paths outside the cone

Example

git add --sparse path/outside/cone

Read file paths from a file instead of command line. Use '-' to read from stdin.

When neededFor scripts with many paths; avoids command-line length limits

Example

git add --pathspec-from-file=files.txt

In -p/-i, show the context between hunks up to the given number of lines, fusing hunks that are close to each other into one (default 0, or diff.interHunkContext). Requires Git 2.51 or newer.

When neededFuses nearby hunks in add -p — needs Git 2.51 or newer

Example

git add -p --inter-hunk-context=5

Used with --pathspec-from-file. Pathspec elements are separated by NUL character instead of newlines. Handles filenames with special characters.

When neededCompanion to --pathspec-from-file for names with newlines or odd characters

Example

git add --pathspec-from-file=files.txt --pathspec-file-nul

Choosing the Right Option

Staging Commands

Understanding which files get staged with different commands

CommandNew FilesModified FilesDeleted FilesScope
git add <file>✓✓✓Specific file only
git add .✓✓✓Current dir & below
git add -A✓✓✓Entire repository
git add -u✗✓✓Tracked files only
git add --no-all✓✓✗Ignores deletions

Interactive Options

Options for fine-grained control over what gets staged

CommandModeGranularityUse Case
git add -pPatchHunk-by-hunkStage specific changes within a file
git add -eEditorLine-by-lineManually edit diff before staging
git add -iMenuFile-by-fileFull interactive staging menu

Preview & Output Options

Options that show information without modifying the staging area

CommandActionModifies IndexPurpose
git add -nDry-run✗Preview what would be staged
git add -vVerbose✓Show files as they're staged
git add --refreshRefresh✗Update stat info only

Special File Handling

Options for handling files that need special treatment

CommandHandlesBehaviorTypical Use
git add -fIgnored filesForce past .gitignoreAdd a file that's normally ignored
git add -NNew filesIntent to add (empty)Track file for git diff without staging
git add --chmod=+xExecutablesSet file modeMake scripts executable
git add --sparseSparse filesOutside coneStage files outside sparse checkout

Advanced Options

Expert-level options for specific scenarios

CommandPurposeWhen to Use
git add --renormalizeRe-apply clean filtersAfter changing .gitattributes line endings
git add --pathspec-from-fileRead paths from fileStaging many files from a list
git add --ignore-errorsContinue on errorsStage what you can, skip failures
git add --no-warn-embedded-repoSuppress submodule warningIntentionally adding nested repos

Workflow Examples

1
git status

Review all changes first

2
git add -A

Stage all new, modified, and deleted files

3
git commit -m "Your message"

Commit all staged changes

1
git add -p

Interactively select only the bug fix changes (press 'y' for bug fix hunks, 'n' for feature hunks)

2
git commit -m "Fix login validation bug"

Commit just the bug fix

3
git add -A

Now stage the remaining feature changes

4
git commit -m "Add dark mode toggle"

Commit the feature separately

1
git status

See what's modified vs untracked

2
git add -u

Stage only modifications to tracked files

3
git status

Verify new files are still untracked

4
git commit -m "Update existing files"

Commit without the new files

1
git add -n .

Dry run shows what would be added

2
git diff

Review the actual changes in detail

3
git add .

If satisfied, stage for real

1
git add dist/bundle.js

This fails - file is ignored

2
git add -f dist/bundle.js

Force add overrides .gitignore

3
git commit -m "Add build for deployment"

Commit the normally-ignored file

Interactive Mode Keys

Keyboard shortcuts for git add's interactive modes. These work in patch mode (-p) and interactive mode (-i).

Patch Mode

-p
yStage this hunk
nDon't stage this hunk
qQuit; don't stage this hunk or any remaining ones
aStage this hunk and all later hunks in the file
dDon't stage this hunk or any later hunks in the file
sSplit the current hunk into smaller hunks
eManually edit the current hunk
?Show help

Interactive Mode

-i
1 / sStatus - show current staging state
2 / uUpdate - select files to stage
3 / rRevert - unstage selected files
4 / aAdd untracked - add new files
5 / pPatch - enter patch mode for selected files
6 / dDiff - show diff of staged changes
7 / qQuit
8 / hHelp

Troubleshooting

File is not being added (ignored)

Cause: The file matches a pattern in .gitignore

Check .gitignore for matching patterns. Use -f to force add if intentional.

git add -f <file>

Permission denied error

Cause: File is locked by another process or you lack read permissions

Close any applications using the file, or check file permissions. Use --ignore-errors to continue with other files.

git add --ignore-errors .

Nothing happens when running git add

Cause: Files may already be staged, or there are no changes to add

Run git status to see current state. Files already staged won't be re-added.

git status

New files not showing in git diff

Cause: Untracked files don't appear in git diff by default

Use git add -N to mark intent-to-add, then git diff will show the content.

git add -N <file> && git diff

Warning about embedded git repository

Cause: You're adding a folder that contains its own .git directory (a nested repo)

Either remove the nested .git folder, use git submodules, or suppress the warning.

git add --no-warn-embedded-repo <folder>

Line endings keep showing as changes

Cause: Inconsistent line endings (CRLF vs LF) between your system and the repo

Configure .gitattributes with 'text=auto' and renormalize all files.

git add --renormalize .

Scripts (e.g. git diff-files) list files as modified but content is unchanged

Cause: File timestamps changed, so the stat info cached in the index is stale

Refresh the index to update stat info without staging. (git status refreshes automatically.)

git add --refresh .

Tips & Pitfalls

Best Practices

  • ✓Review changes before staging: Use 'git diff' to see what you're about to stage, or 'git add -p' to review interactively.
  • ✓Stage related changes together: Group logically related changes in a single commit. Don't mix bug fixes with new features.
  • ✓Use 'git add -p' for large changes: When you've made multiple unrelated changes to a file, use patch mode to stage them separately.
  • ✓Check git status frequently: Always run 'git status' before and after staging to verify what will be committed.
  • ✓Be cautious with 'git add .': This stages everything in the current directory. Consider using specific filenames or 'git add -p' for more control.
  • ✓Set up .gitignore first: Before your first 'git add', ensure .gitignore is configured to exclude build artifacts, dependencies, and sensitive files.
  • ✓Use dry-run for verification: When unsure, use 'git add -n .' to preview what would be staged without actually staging.
  • ✓Stage deletions explicitly: Remember that deleting a file locally doesn't stage the deletion. Use 'git add -u' or 'git add -A' to include deletions.

Common Mistakes

  • ✗Using 'git add .' vs 'git add -A': Both stage new, modified and deleted files, but '.' only covers the current directory down, while '-A' covers the entire working tree.
  • ✗Forgetting that 'git add' does not commit changes. The files are only staged and require 'git commit' to be recorded.
  • ✗Adding files that should be ignored. Always check your .gitignore before running 'git add -A' or 'git add .'
  • ✗Not reviewing changes before staging. Use 'git add -p' for a more careful review of what you're staging.
  • ✗Confusing 'git add -u' with 'git add -A': The '-u' flag only stages modifications and deletions of tracked files, not new untracked files.

FAQ

A:

Since Git 2.0 both stage new, modified and deleted files. The difference is scope: 'git add .' covers only the current directory and subdirectories, while 'git add -A' covers the entire repository, regardless of your current directory.

A:

The file is likely listed in .gitignore. Check with 'git check-ignore <file>'. If you need to add it anyway, use 'git add -f <file>' to force.

A:

Yes! Use 'git restore --staged <file>' (or 'git reset HEAD <file>' in older Git versions) to unstage a file without losing your changes.

A:

Use 'git add -p <file>' to enter interactive patch mode. Git will show you each change (hunk) and let you choose which ones to stage with y/n.

A:

This usually happens due to line ending changes (CRLF/LF) or file permission changes. Check 'git diff' to see what changed. Configure .gitattributes to handle line endings.

A:

Add the files you want to exclude to .gitignore first, then use 'git add .'. Alternatively, add files individually or use patterns like 'git add *.js'.

A:

It tells Git you intend to add the file later. The file becomes tracked (appears in git diff) but the content isn't staged yet. Useful for reviewing new files before committing.

A:

No, Git only tracks files, not directories. The common workaround is to add a .gitkeep or .gitignore file inside the empty directory.