75

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
  • 7,191
  • 10
  • 60
  • 105
Chapsterj
  • 6,483
  • 20
  • 70
  • 124

3 Answers3

122

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 Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
  • 2
    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 Kulinkovich Jul 14 '18 at 13:27
  • @clabe45 you can find more details in link provided in answer – Alex Kulinkovich Jul 14 '18 at 13:28
  • 3
    @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
  • 92,517
  • 154
  • 335
  • 470
  • 26
    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
  • 3
    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".` –  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,639
  • 36
  • 41