The question
How do I generally .gitignore Log/log folders, but include my specific NetTerminal/Log/
folder and contents below it?
Background
We're using Azure DevOps and have a repository with the .gitignore template file "VisualStudio".
The .gitignore file has by default this line:
[Ll]og/
It happens that we have a Visual Studio project with a log class in: NetTerminal/Log/Log.cs
.
The folder and the file is being ignored by git because of the default line above.
The NetTerminal folder is not below another Log folder, so that's not the reason.
I have tried adding any of the below, but they doesn't do anything, the file doesn't show up in Sourcetree's Uncommitted Changes list.
!NetTerminal/Log/
!NetTerminal/Log/*
!NetTerminal/Log/Log.cs
If I add a * to the default, as follows, now my custom file is found - but if I create a new Log folder somewhere else and add a file into it, it is also showing up, so the [Ll]og/* line is kind of ignored?
[Ll]og/*
Please advice? :-)
Answer
As this question has been closed, I'm writing the answer to my question here. Maybe the question can be reopened, then I'll move this to a real answer.
The answer to this problem lies in one line of the manual for gitignore files:
If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.
I knew about /zyx being (relative to the .gitignore file, so file or folder "zyx" is located in the same folder as the .gitignore file. I also knew about zyx/ meaning zyx IS a folder, not a file. So I made the mistake to put !NetTerminal/Log believing it could unignore my Log folder located in NetTerminal folder somewhere in the folder structure. But no can do, specifing a folder structure to match against automatically makes it relative to the .gitignore file. I find this sad. Prefixing with / clearly states that this is located in relation to the .gitignore file.
So first I thought I had to put the full path relative to the .gitignore file, making it volunerable if we would change our folder structure.
BUT, "**/" to the rescue! From the manual:
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".
So, the solution is:
[Ll]og/
!**/NetTerminal/Log/
With this, new Log folders (with files inside) is ignored and my NetTerminal/Log file is included.
YEAY!