What I want to do is to list all gitignore
rules inside a folder. Assume the following folder structure:
project/
project/folderA/
Now if I want some gitignore
rules for my whole project, I'd put it in project/.gitignore
, but I could also add some rules to project/folderA/.gitignore
, which as I understand, would apply to project/folderA
and its subfolders only.
Now assume I do have some rules in both .gitignore
files, and I'm in project/folderA
, how can I list all gitignore
rules that apply here? I need to see all the union of the ones inherited from parent folders and the ones existing in the current folder.
You can use the following command:
git check-ignore *
which would list you everything directly under the current folder which is ignored, and if you add a -v
option, it would also show you the rule which has caused it to be ignored.
What I need is not to give an example and see if it's ignored or not, I simply need to see all the rules applying to the current folder.
EDIT: Some suggest that the .gitignore
file in the subfolder overrides the parent and only that file needs to be checked. I've tested with git version 2.13.3
and this is not how it works. If I have patternA
in parent folder, it IS ignored in the subfolder even if I don't have that same pattern in the subfolder's .gitignore
file.
EDIT 2: Consider the following working example:
[user@machine tmp]$ mkdir test
[user@machine tmp]$ cd test/
[user@machine test]$ git init
Initialized empty Git repository in /tmp/test/.git/
[user@machine test]$ echo "a">.gitignore
[user@machine test]$ mkdir p
[user@machine test]$ cd p
[user@machine p]$ echo "b">.gitignore
[user@machine p]$ echo "c">.gitignore
[user@machine p]$ touch a
[user@machine p]$ touch b
[user@machine p]$ cd ..
[user@machine test]$ touch a
[user@machine test]$ touch b
[user@machine test]$ git add .
[user@machine test]$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: b
new file: p/.gitignore
[user@machine test]$ cd p
[user@machine p]$ git list-git-ignore-rules-applied-here <-- hypothetical
As you can see, the only rule in the parent folder is a
, and the two rules in the p
folder are a
and b
, hence niether of those created files being tracked after git add .
. Now the last line of that example code is what I need, which should give me the output:
a
b
c