Keval
Keval

Reputation: 1602

Gitignore everything except one file extension

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

Upvotes: 2

Views: 564

Answers (3)

Garulf
Garulf

Reputation: 21

Seems order is important here and I was able to make it work perfectly with this:

# Ignore everything
*
# ignore any files
*.*
# Only include root .gitignore
!/.gitignore
# re-include sub-directories
!*/
# ignore all sub-sub-directories
/*/*/*
# include docker-compose
!*/docker-compose.yml

Thanks for the help!

Upvotes: 0

SwissCodeMen
SwissCodeMen

Reputation: 4875

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

Upvotes: 1

allquiet
allquiet

Reputation: 11

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

Upvotes: 1

Related Questions