Coding

git add -A: What It Does vs git add . and git add -u

git add -A (long form git add --all) stages every change in the whole working tree: new files, modified files and deleted files. It differs from git add -u, which skips new files, and from git add ., which only looks at the current directory and below.

The option is an uppercase A. Git options are case-sensitive, and the git add documentation lists -A, not -a. If you typed lowercase because you remember git commit -a, that is a different command with different behavior, covered further down.

What git add -A does

The Git documentation describes -A as updating the index "not only where the working tree has a file matching pathspec but also where the index already has an entry." In plain terms, it adds, modifies and removes index entries so the index matches the working tree.

When you give no path, -A covers all files in the entire working tree. The docs add that old versions of Git limited this to the current directory and its subdirectories, so on a current install you can run it from any subfolder and it still sees the whole repository.

git add -A
git add --all

Both lines do the same thing. Nothing is committed yet. The change is only staged, so you still need git commit afterwards.

git add -A vs git add . vs git add -u

All three commands stage changes, but they differ in which kinds of change they pick up and where they look.

CommandNew (untracked) filesModified filesDeleted filesScope
git add -AYesYesYesWhole working tree
git add -uNoYesYesWhole working tree, tracked files only
git add .YesYesYesCurrent directory and below

The -u (--update) option updates the index "just where it already has an entry matching pathspec." It removes as well as modifies entries, but adds no new files. That makes it the right choice when you have edited existing files and do not want a stray scratch file to slip in.

For git add ., the dot is a pathspec naming the current directory. The documentation says that giving a leading directory name updates the index to match the current state of that directory as a whole, which includes files added, modified and removed inside it. So on a modern Git, git add . run from the repository root gives the same result as git add -A. Run from a subdirectory, it stages only that subdirectory, while git add -A still stages everything in the repository.

The documentation also notes that older versions of Git ignored removed files when given a pathspec. If you ever need that old behavior, --no-all (also spelled --ignore-removal) stages new and modified files but skips deletions.

Worked example

Suppose a repository where you edited README.md, deleted old.txt, and created notes.txt. Check the state first:

git status -s

Example output:

 M README.md
 D old.txt
?? notes.txt

In the short format, the first column is the index status and the second is the working tree status. A space in the first column means nothing is staged yet. M is modified, D is deleted and ?? is untracked.

Now run git add -u and check again. Example output:

M  README.md
D  old.txt
?? notes.txt

The modification and the deletion moved to the first column, which means they are staged. notes.txt is still untracked, because -u never adds new files. Run git add -A instead and notes.txt becomes A notes.txt as well.

What git add -A will not add

Ignored files stay out. The documentation says git add "will not add ignored files by default" and that --force overrides this. If a file you expected to see staged is missing and git status never lists it, check your .gitignore rules first. If the file was already tracked before you ignored it, the rule has no effect on it, which is covered in why .gitignore is not working.

Empty directories are not recorded either, because Git tracks file content, not folders. A directory shows up in a commit only when it contains at least one file that is staged.

The risk of staging everything

git add -A does not ask questions. Anything in the working tree that is not ignored goes into the index: log files, build output, a .env file with credentials, a large binary you downloaded for testing. Two habits help:

  • Run git status before and after. The long format lists staged files under "Changes to be committed" and new ones under "Untracked files", so you can see exactly what would go in.
  • Preview with git add -n -A. The -n (--dry-run) option does not add anything. It only shows whether files exist and will be added or ignored.

If a secret does get committed, deleting it in a later commit is not enough. See how to remove a secret from Git history. To keep such a file out in the first place, read whether .env should be in Git.

Stage only what you mean to: paths and patches

You do not have to stage everything. Name the files, or a directory:

git add src/app.py README.md
git add src/

Fileglobs work too. The docs give git add Documentation/\*.txt, with the asterisk quoted so that Git expands it and includes files in subdirectories. If you let the shell expand the asterisk instead, it only lists files in that one directory.

To review changes piece by piece, use -p (--patch). It lets you "interactively choose hunks of patch between the index and the work tree," giving you a chance to review the difference before staging. This is the safest way to build a clean commit from a messy working tree.

git add -p

How git commit -a differs

The lowercase -a exists on git commit. Its documentation says it automatically stages "files that have been modified and deleted, but new files you have not told Git about are not affected." That is the same coverage as git add -u followed by a commit, in one step:

git commit -a -m "Fix typo in README"

A new file needs git add first, or git commit -a will leave it out. If you committed and then realized you left something out, undoing the last commit lets you restage and try again.

Undo a git add -A

If you staged too much, you can unstage without losing your edits:

git restore --staged .
git restore --staged path/to/file

The first line empties the index and the second unstages one file. Your working tree is not touched in either case. Run git status afterwards to confirm that everything is back in the "Changes not staged for commit" and "Untracked files" sections, with nothing left under "Changes to be committed".

Which one to use

Use git add -A when you want the whole repository state in the next commit, including new files and deletions. Use git add -u when you only touched files Git already knows. Use git add . when you want the current directory only. When the commit needs to stay focused, name the files or use git add -p.

Frequently asked questions

Is git add -a the same as git add -A?
No. Git options are case-sensitive, and the git add documentation lists the uppercase -A (long form --all). The lowercase -a belongs to a different command: git commit -a stages tracked files that were modified or deleted, then commits. It does not add new files.
Does git add -A add files listed in .gitignore?
No. The git add documentation says the command will not add ignored files by default. You need the --force option to add an ignored file on purpose.
Which is safer, git add -A or git add -p?
git add -p is safer when you want to review changes. It walks through each hunk and asks whether to stage it, so you decide what goes into the commit. git add -A stages everything at once without showing you anything.
How do I undo git add -A before committing?
Run git restore --staged . to remove everything from the index while keeping your edits in the working tree. To unstage one file, name it: git restore --staged path/to/file.

Sources