13

git animals had this series of commands:

git init
git add *
git commit -a -m ‘initial commit and release!’

What does git add * do compared to git add . (which I normally do) are they the same?

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
  • * usually means "Match Everything" and . means "this current directory," which by default is done recursively. – TylerH4 Jun 10 '12 at 18:33
  • @TylerH4: But if the expansion of `*` includes directories they will be added recursively by git in any case. – CB Bailey Jun 10 '12 at 18:34

2 Answers2

17

git add * will add all the paths that are the result of the shell expansion of * whereas git add . will tell git to add the current directory.

git add * won't add paths that begin with a . as the shell expansion of * considers these to be "hidden" paths.

git add * will also fail if any expanded path is currently being ignored by git because git considers it an error if you explicitly specify an ignored path without passing the -f (force) flag to show that you really want to add an ignored path.

If you get git to expand the glob (git add '*') it will add "hidden" files and skip over ignored files. It would work the same as git add . in this case.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
2

By default passing a directory to git add will recursively add its sub-directories and files.

The wildcard * will be expanded by your shell as files' names below current directory.

In Unix . refers only to the current directory, so these two commands are usually equivalent. Though * is processed by the shell and . is processed by git, they usually do the same thing.

However, note that * in shell expansion usually doesn't include hidden files (files that has a . prefix, e.g. .formatter.yml). Also, the shell expansion doesn't respect git ignored files in .gitignore.

shouya
  • 2,863
  • 1
  • 24
  • 45
  • 4
    Since `*` is processed by the shell, it does not take into consideration `.gitignore`, whereas `.` does. – wisbucky Sep 18 '14 at 22:39
  • Good point @wisbucky. If you have a python project and if you do ` git add * `, the "pycache" will be added despite explicitly specifying in .gitignore – Seth P Jul 20 '22 at 14:53