2

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
adrin
  • 4,511
  • 3
  • 34
  • 50
  • You mean you want rules that explicitly apply to the folder or *could* apply to the folder, assuming you put a matching file in there (ie. rules that doesn't care about folders) ? – Lasse V. Karlsen Jul 31 '17 at 09:45
  • Why do you need this? Which problem are you trying to solve here? – Lasse V. Karlsen Jul 31 '17 at 09:45
  • I've cloned a repo, and not I want to add a folder with a bunch of data in it. I need to know if there is any pattern resembeling `data` in my `gitignore` rules, that I can use and name my data folder as such. I also don't want to change any `.gitignore` files if I don't have to. – adrin Jul 31 '17 at 09:59

1 Answers1

-3

As the manual states:

"Patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file."

So only one .gitignore file is applied on your subdirectory, so for the rules applied you would only need to read out a single file.

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Swifting
  • 449
  • 1
  • 5
  • 19
  • I know that's what the manual says. But it's vague, and it doesn't work as you suggest. – adrin Jul 31 '17 at 10:48
  • If you add new lines to a .gitignore in a subfolder, the lines of the parent folder are still applied since git will continue to track any files that are already being tracked. See: https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore?rq=1 – Swifting Jul 31 '17 at 11:13
  • The files being tracked by git have nothing to do with the lines being applied as the rules, and even if that made sense, your answer is still wrong. – adrin Aug 01 '17 at 08:23