1

git add *.c is supposed to add matching files only from the current directory, not from its subdirectories. I noticed that if no file matches the searched pattern in the current directory, git add *.c is adding matching files from subdirectories.

Does anybody know how to avoid this behavior?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
tkarra
  • 541
  • 3
  • 8

2 Answers2

3

When git processes wildcards it is supposed to match files at any level. The reason that you are seeing this behaviour is because the shell expands *.c to an explicit list of files if that wildcard matches in the current directory. In this case Git sees and explicit list of .c files, not a wildcard.

If the shell fails to expand *.c because no files in the current directory match that pattern then the wildcard pattern is passed unexpanded to Git which performs the expansion itself and matches in subdirectories.

If you are using bash you can use shopt -s nullglob to make the shell expand the wildcard to empty, or shopt -s failglob to produce an error and not run git add if the pattern doesn't match.

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

git add "/*.c" should do the trick.

The leading slash tells Git to look in the current folder only.

Note that you should always quote this or the shell might try to expand the glob pattern before Git sees it which would lead to all kinds of flaky behavior.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820