1

I'm trying to ignore everything except docker-compose.yml files wherever they may be:

- root
  - .git/
  - .gitignore
  - folderA
    - docker-compose.yml
  - folderB
    - .git/
    - data/
    - docker-compose.yml

In the above directory example, the only things that git should track are both docker-compose.yml files and the .gitignore file.

I have tried the following .gitignore:

/*
!.gitignore
!docker-compose.yml

and also

/*
!.gitignore
!**docker-compose.yml

and also

/*
!.gitignore
!**/docker-compose.yml

and also

/*
!.gitignore
!/*/docker-compose.yml

I've also tried the above options with just * at the top instead of /*

All of which result in only the .gitignore file being tracked.

I'm probably missing something really silly and small but hey ho. Thanks in advance.

UPDATE

I have come closer to a solution with:

*
!/.gitignore
!*/
!*/docker-compose.yml

It is now successfully including all the docker-compose.yml files, however it is now also including sub-repositories and some random files such as root/nextcloud/data/html/core/vendor/zxcvbn/LICENSE.txt

TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37

2 Answers2

1

If you use this you can ignore all 3rd level subfolders

/*/*/*

So following your example structure, the updated included files would be all those present in the root folder (1st level) and all those present in the folders ".git", "folderA" and "folderB" (2nd level). Meanwhile the folders .git/ and data/ (3rd level) inside "folderB" would be ignored. If ".git/" was empty, you would get:

- root
  - .gitignore
  - folderA
    - docker-compose.yml
  - folderB
    - docker-compose.yml

If you also want to exclude all the other files besides the docker-compose.yml, you would need to exclude all extensions, and include only the docker-compose.yml and gitignore, like so:

/*/*/*
*.*
!.gitignore
!docker-compose.yml
allquiet
  • 11
  • 4
0

You can ignore all with

*

(Like you've already done)

Now you must know the whole path to all files, where you don‘t want to ignore. The path beginns in .gitignore root, so your root folder is unneccessary

Updated folder-structure:

- .git/
- .gitignore
- folderA
  - docker-compose.yml
- folderB
  - .git/
  - data/
  - docker-compose.yml

As example to your directory-structure, you can add following two entries:

!folderA/docker-compose.yml
!folderB/docker-compose.yml
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • I want this to be generic over all folderX rather than specifying each one individually – TheChubbyPanda Nov 23 '21 at 06:42
  • As far as I know, that is not possible. Look here in the [docu](https://git-scm.com/docs/gitignore#_pattern_format) about `gitignore`. This is not implemented because performance reasons. Then for searching in all subdirectories needs a lot of power and time. So you can only do that as I mentioned in the answer: By entering every path to the file where you *don't* want to ignore. – SwissCodeMen Nov 24 '21 at 12:16
  • What about if I know that I only want files one directory down? Sureley there has to be a way to do this. I do see your point about time complexity though – TheChubbyPanda Nov 24 '21 at 13:39
  • No, you can't (as far as I know...). You must write for every case following: `!folder` followed by `folder/*` and at least `!folder/file.txt` – SwissCodeMen Nov 24 '21 at 19:20