2

I am aware this was asked here and answered, however no answer covers when the ignoring line uses wildcards...

I have the following line in .gitignore:

**/_secrets

Now I would like to add one exception to this using the ! syntax:

!**/_secrets/not_a_secret.txt

However this does not seem to work.

What I've tried:

!**/not_a_secret.txt
!**/_secrets/not_a_secret.txt
!(**/_secrets/not_a_secret.txt)

I've also tried to change **/_secrets to **/_secrets/ still does not work, all files in the _secrets folder are ignored, with no exception of not_a_secret.txt file.

What am I missing?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • 1
    Fwik the order matters. Make sure you first first have the exclusion, then the exception. There shouldn't be anything different with your case than with the answer you linked. The fact that you have ** shouldn't matter here I think. – bolov Nov 15 '21 at 05:43
  • 1
    Try `**/_secrets/*` – aerial Nov 15 '21 at 05:46
  • 1
    Also make sure you read the second answer to that question. It's important. – bolov Nov 15 '21 at 05:47
  • 2
    You *must not* ignore the `_secrets` directory itself, if you want Git to look *inside* the `_secrets` directory. That is, you want `**/_secrets/*` as @aerial301 suggested. Git will have to open and read each `_secrets` directory before decidng whether to ignore some file *in* that directory. The `*` will then ignore all those files, but the later `!` lines will override that for the specified files. – torek Nov 15 '21 at 06:16
  • Many thx for All. Now I tried `**/_secrets/*` and `!**/_secrets/not_a_secret.txt` in *both order* the file still ignored. I've also tested, if I comment out `**/_secrets/*` no files are ignored, so that is the line which effectively ignores the file and `!**/_secrets/not_a_secret.txt` makes no exclusion, with other words seems to be not working – g.pickardou Nov 15 '21 at 07:21

1 Answers1

2

The general rule of gitignore is simple:

It is not possible to re-include a file if a parent directory of that file is excluded.

To exclude files (or all files) from a subfolder of an ignored folder f, you would do:

**/_secrets/
!**/_secrets/**/
!**/_secrets/**/not_a_secret.txt

Meaning: you need to whitelist folders first, before being able to exclude from gitignore files.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Many thx. I've tried exactly this three lines in an empty repo, with a .gitignore with only those three lines for the sake of isolate my issue. The `not_a_secret.txt` **is still ignored**. Just for the check: if I comment out the `_secrets/` line, then no files in the folder are ignored.... – g.pickardou Nov 15 '21 at 13:11
  • I meant a freshly created repo with two files: `/_secrets/mysecret.txt` and `/_secrets/not_a_secret.txt` – g.pickardou Nov 15 '21 at 13:19
  • @g.pickardou I just amended the rules (and tested them in a dummy repo): they are now working as expected. – VonC Nov 15 '21 at 13:34
  • Many thx for you valuable time I do not want to abuse... I give up, and going to find a workaround, moving the file to other folder... still the same for me with the amended rule. – g.pickardou Nov 15 '21 at 13:59
  • @g.pickardou Don't give up: I promise: those three rules are doing exactly what you requested in your question. No workaround required. – VonC Nov 15 '21 at 14:00
  • @g.pickardou Unless... what is your Git version? What does `git version` return? – VonC Nov 15 '21 at 14:01
  • git version 2.30.2.windows.1 – g.pickardou Nov 15 '21 at 14:02
  • @g.pickardou Perfect: it should work just fine with this version. – VonC Nov 15 '21 at 14:33