What is the difference between git add * and git add .?
-
*
is a bash glob. It will expand to all files in the directory you're in, excluding dotfiles (files that start with a dot (.
)). -
.
means the current directory.
The result can be quite different depending on the content of your directory and if you have a .gitignore
file.
Consider you have the following files and directories in your directory:
.dotfile-that-must-be-committed
.git/
.gitignore
some-file-that-is-in-gitignore
some-other-file
When you run git add *
, the glob is expanded before the git command runs. So what the git command receives is the following:
git add some-file-that-is-in-gitignore some-other-file
This causes four problems here.
- Git will complain that
some-file-that-is-in-gitignore
cannot be added and will ask you to add the force (-f
) argument if you really want to add it. - both
.dotfile-that-must-be-committed
and.gitignore
were not added as*
does not expand to dotfiles. - If you have deleted files in the directory you are in,
*
can never expand to those, so deleted files will not get staged. - If you have renamed files in the directory you are in,
*
can never expand to the old name but it will expand to the new one. So what git will see is that you added a new file, and since the old name did not get staged you will end up with the same file existing twice, with the old name and the new name.
However, running git add .
tells git to add the current directory you're in without specifying which file to add. In this case, git will check the .gitignore
file and recursively add all files not mentioned in .gitignore
.
In summary:
- You should use
git add .
notgit add *
.
Even better:
- Use
git add full/file/paths
so you don't add stuff by mistake that isn't ready to be added.
And even much better:
- Use
git add -p
to review your changes and pick and choose which patches you want to be added.
Something else not pointed out by current answers would be that git add *
will not notice if you've deleted or renamed any files, but git add .
will.