git add * (asterisk) vs git add . (period)
Solution 1
add *
means add all files in the current directory, except for files whose name begin with a dot. This is your shell functionality and Git only ever receives a list of files.
add .
has no special meaning in your shell, and thus Git adds the entire directory recursively, which is almost the same, but including files whose names begin with a dot.
Solution 2
*
is not part of git - it's a wildcard interpreted by the shell. *
expands to all the files in the current directory, and is only then passed to git, which add
s them all.
.
is the current directory itself, and git add
ing it will add it and the all the files under it.
Solution 3
git add -A
(--all) Adds everything, so that everything in your folder on disk is represented in the staging areagit add .
Stages everything, but does not remove files that have been deleted from the diskgit add *
Stages everything, but not files that begin with a dot & does not remove files that have been deleted from the diskgit add -u
(--update) Stages only Modified Files, removes files that have been deleted from disk, does not add newgit add <file name 1> <file name 2>
Adds only certain file(s)
Solution 4
For clarity, I put the answer in the table below:
Additional notes (inspired by the @reka18 comment):
Note 1. git add -A
and git add -u
commands performed without additional parameters would be additional refinement (subdirectory or mask indication for the file name) work in the range of the entire working directory (also if we execute the command in the working subdirectory of the directory).
Note 2. The .
and *
are respectively the directory path (current directory) and the wildcard, which clarify the path of the command. For example, if the git add .
or git add *
command is executed in some subdirectory of a working directory, then their action is only used within this subdirectory, not the entire working directory.
Note 3. The git add -A
and git add -u
commands can be further refined by adding a path or mask for files, for example, git add -A app/controllers
or git add -u app\styles\*
.
Solution 5
Using the dot .
in the shell usually means "the current directory".
When you use the asterisk *
on a shell a feature called file-globbing
is utilized. E.g. on bash the function glob()
is doing just that. The manpage for glob (man 7 glob
) states:
DESCRIPTION
Long ago, in UNIX V6, there was a program /etc/glob that would expand wildcard patterns. Soon afterward this became a shell built-in. These days there is also a library routine glob(3) that will perform this function for a user program.
Wildcard matching
A string is a wildcard pattern if it contains one of the characters '?', '*' or '['.
Globbing
Globbing is the operation that expands a wildcard pattern into the list of pathnames matching the pattern.
That means when you pass arguments to any program on the commandline that contain '?'
, '*'
or '['
, first globbing expands the wildcard pattern into a list of files and then gives these files as an argument to the program itself.
The difference in meaning between 'git add .'
and 'git add *'
is clearly described by Denis:
git add
expects a list of files to be added. In the above example the shell expands *
or .
respectively and gives the result as a parameter to git add. Now the difference is that with git add .
git will expand to the current directory whereas git add *
triggers file globbing and such expands to all files and directories that do not start with a dot.
Related videos on Youtube
Tyler Youngblood
Founder of ProjectsInMetal.com, a hobby metalworking website. Web Developer at Richland Library. Former Computer Science Instructor at Highline College.
Updated on May 24, 2020Comments
-
Tyler Youngblood almost 4 years
I'm new to git and I have a question about adding files in git. I have found multiple stackoverflow questions about the difference between
git add .
andgit add -a
,git add --all
,git add -A
, etc. But I've been unable to find a place that explains whatgit add *
does. I've even looked at the git add man page, but it didn't help. I've been using it in place ofgit add .
and my co-worker asked me why. I didn't have an answer. I've just always usedgit add *
.Are
git add .
andgit add *
the same? Does one add changed files from the current directory only, while the other adds files from the current directory and subdirectories (recursively)?There's a great chart listed on one of the other stack questions that shows the difference between
git add -A
git add .
andgit add -u
, but it doesn't havegit add *
.Note: I understand what it means to use the asterisk as a wildcard (add all files with a given extension). For example,
git add *.html
would add all files that have a.html
extension (but ignore.css
,.js
, etc).Thanks for the help!
-
David over 8 yearsWhere's that chart from? I just tried
git add .
again, and it staged a deleted file no problem, unlike theX
in that row would suggest. -
jerry almost 8 years@David That image is from this answer and applies to older versions of git.
-
Hannes Schneidermayer almost 7 yearsPicture outdated! Git 2.x is different: i.stack.imgur.com/KwOLu.jpg
-
-
Tyler Youngblood over 9 yearsso
git add .
adds all files, folders, and subfolders, including .gitignore and anything else beginning with a dot, whilegit add *
would add any files, folders, and subfolders, except those beginning with a dot? Is that accurate? -
Tyler Youngblood over 9 yearsSo would there every be a reason to use the asterisk? Is there any advantage to using it instead of a period? Or vice versa? I'm sure I saw it in a tutorial. I wouldn't have known to use it otherwise. I'm not much of a command line guy (as you've undoubtedly guessed).
-
Denis over 9 yearsThat is indeed correct. Also,
git add *
would still add files beginning with a dot if they are in a subdirectory. -
Mureinik over 9 years
*
avoids hidden files (i.e., files that their name begins with a.
). In any event, if you aren't adding specific files, I'd just usegit add -u
(orgit add -A
if you're creating new files). -
Tyler Youngblood over 9 yearsSince you both answered my question I had trouble deciding who to give credit to. I chose Denis below because he has less rep than you. So I figured giving him the green check would benefit him more than it would benefit you. I hope that makes sense? But I really appreciate both explanations. Thanks!
-
rosuav almost 8 years
git add .
also respects .gitignore, whereasgit add *
will throw an error if any non-dot-files are gitignored. Much better to usegit add .
thangit add *
. -
Radmation over 7 yearsTested - not accurate in my case - git add * works for my .htacess files - any ideas why?
-
Denis over 7 years@Radmation, that's probably because you have no other files in the directory. Your shell does not expand '*' if there are no matching files. Then, git expands it on its own, with different rules, ignoring the leading dot.
-
Radmation over 7 years@Denis There are lots of files in that directory including an index.php page and many others as well as other directories.
-
Denis over 7 years@Radmation, then I have no further hypotheses without seeing your setup and investigating it manually. Sorry. :)
-
torek over 5 yearsWorth noting: if invoking Git on DOS/Windows from CMD.EXE, it's Git, not the shell, that expands the
*
. In this case Git will find dot-files. -
Thor84no over 5 years@torek that's because Windows does not consider dot-files to be hidden. CMD will probably not include files with the hidden attribute, but I don't use it so I can't verify.
-
torek over 5 years@Thor84no: Git will find the dot-files even on a Linux system, if you quote the
*
to protect it from the shell. It's not a matter of the hidden bit, it's just that Git's compiled-in rules differ. -
reka18 over 5 yearsSo as of Git v2.x
git add -A
andgit add .
are identical? -
simhumileco over 5 yearsThank you @reka18, for a very good question. It inspired me to complete my answer... The answer to your question: If you call it in the working directory, no, but if in a subdirectory, then yes (
git add -A
applies to the entire working directory andgit add .
always the current directory). -
msoutopico over 3 yearsThe answer is a bit confusing, since the former method says "all files in the current directory" whereas the latter says "the entire directory recursively". One could think that the former (i.e.
add *
) only adds files (not folders) in the first level, but not recursively. -
Duke almost 3 yearsThis is the best answer for me. The other ones are all a bit fuzzy, though boiling down to the same after all.
-
simhumileco almost 3 yearsThank you @Duke! :)
-
mawall over 2 years
git add
without flags does not ignore deleted files (anymore). This behavior changed quite a while ago. Also see here: git-scm.com/docs/git-add#Documentation/…