$ git version
git version 2.9.3.windows.1
I seem to be having trouble with the + (one or more) pattern matcher in .gitignore. Here is a test with +
and *
used as a pattern qualifier for digits embedded in the file name. I want to keep Test.txt in source control and ignore the Test[0-9]+.txt files.
Directory:
$ ll | grep Test
-rw-r--r-- 1 asdf 1049089 0 Apr 5 15:55 Test.txt
-rw-r--r-- 1 asdf 1049089 0 Apr 5 15:55 Test123.txt
-rw-r--r-- 1 asdf 1049089 0 Apr 5 15:55 Test1235.txt
-rw-r--r-- 1 asdf 1049089 0 Apr 5 15:55 Test124.txt
Git ignore file contents Test 1:
Test[0-9]+.txt
Test 1:
$ git status | grep Test
Test.txt
Test123.txt
Test1235.txt
Test124.txt
This should have filtered out the 2nd through 4th files.
Git ignore file contents Test 2:
Test[0-9]*.txt
Test 2:
$ git status | grep Test
Test.txt
This should have filtered out ALL of the files.
Why is this happening?
The .gitignore file is using globbing, not regular expressions. So using +
is just not valid.
So your second attempt is actually closer to what you want.
Test*.txt
This would filter out files starting with Test
and ending with .txt
with anything in between. But it would also catch Test.txt
itself because a *
in glob can be zero-width.
What you have may be the simplest option.
Test[0-9]*.txt
That will match Test
followed by any digit, followed by anything (including nothing), followed by .txt
.
So it is imperfect, in that it will also match on
Test1foo.txt
Which has non-numeric text past the digit. But how specific your requirement is will dictate this. You can also just add a few patterns, like
Test[0-9].txt
Test[0-9][0-9].txt
Test[0-9][0-9][0-9].txt
Test[0-9][0-9][0-9][0-9].txt
Test[0-9][0-9][0-9][0-9][0-9].txt
If you really have to be sure it's all digits, then just pick how many digits, and use multiple entries like that to make it a very specific match.