59

Is there a way to have a git ignore file to ignore all files with the name test in them?

I have files like this:

 - css/test.css 
 - js/subfolder/test.js 
 - lib/main/sub/test.html

and so on.

I want git to avoid adding or committing any files with the name test.

JoeG
  • 6,828
  • 8
  • 57
  • 102
Chapsterj
  • 6,193
  • 19
  • 67
  • 123

3 Answers3

99

From git docs

A leading "**" followed by a slash means match in all directories. For
example, "**/foo" matches file or directory "foo" anywhere, the same
as pattern "foo". "**/foo/bar" matches file or directory "bar"
anywhere that is directly under directory "foo".

For your case:

**/[Tt]est*

it also matches both upper and lower case.

Alex.K.
  • 3,870
  • 14
  • 43
  • 49
  • 1
    What does the trailing `*` do? – clabe45 Jul 14 '18 at 13:02
  • @clabe45 Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning: 1) A leading "**" followed by a slash means match in all directories. 2) A trailing "/**" matches everything inside. 3) A slash followed by two consecutive asterisks then a slash matches zero or more directories. 4) Other consecutive asterisks are considered invalid. – Alex.K. Jul 14 '18 at 13:27
  • @clabe45 you can find more details in link provided in answer – Alex.K. Jul 14 '18 at 13:28
  • @clabe45 a * is a special character which means any number (including zero) of any character. So in this case it will match `test.json`, `test.py`, `testosterone`, `testimony.txt.jpg.zip`, or even just `test`. You might try doing `**/[Tt]est.*` with a period, although that will not catch files like `testClient.js`. – Mark Peschel Jul 01 '21 at 11:13
28

Update .gitignore with test*

Also, read this for more information.

James Raitsev
  • 87,465
  • 141
  • 322
  • 462
  • 21
    Why so many votes? Correct seems `**/..` with the two beginning stars. The link does not refer to the 2 stars either. `test*` ignores it only in the base dir. – Timo Jul 20 '18 at 08:08
  • @Timo test* ignores recursively. If you prefix with a /, eg /test*, then it would only ignore in the base dir – Eric Zhang Oct 11 '18 at 17:57
  • I'm getting the same results as Timo - with or without leading slash, a filename followed by a star, e.g. test*, does not get ignored outside of the base dir. – Bytech Nov 06 '18 at 14:31
  • 2
    From `man gitignore`: `A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".` – Rakib Mar 25 '19 at 07:12
  • This does not answer the question. – Qumber Jun 04 '20 at 06:34
4

Try adding the pattern to .gitignore of test.*

Add it and do a git status with some files like you mentioned above.

If it works you're good.

Dave G
  • 9,441
  • 34
  • 40