Search code examples
gitgitignore

how can I use git to track folder structure?


I try to use .gitkeep but no matter what I tried it just does not work.

For example I had a folder structure like this:

.
├── a
│   ├── aa
│   │   ├── aaa
│   │   │   ├── .gitkeep
│   │   │   └── test.txt
│   │   ├── .gitkeep
│   │   └── test.txt
│   ├── .gitkeep
│   └── test.txt
├── b
│   ├── bb
│   │   ├── bbb
│   │   │   └── test.txt
│   │   ├── .gitkeep
│   │   └── test.txt
│   └── test.txt
└── .gitignore

I had the .gitignore like this:

*
!a/.gitkeep
!*/.gitkeep
!**/.gitkeep
!.gitignore

The idea is to keep all the folders with .gitkeep. Surprisingly, no folders will be tracked with this config.

Can anyone tell me why this does not not work?


Solution

  • Can anyone tell me why this does not not work?

    For that, you have git check-ignore -v

    Second, it is not possible to re-include a file if a parent directory of that file is excluded

    And * would ignore folders as well, making all the other exclude rules moot.

    To exclude the .gitkeep only (and ignore the content of the folder which are kept), ignore files only, then whitelist folders, finally exclude (out of all the ignored files) the .gitkeep ones.

    /**
    !/**/
    !.gitkeep
    

    But if the goal is to not ignore any files in folders where there is a .gitkeep in it, then you would need to exclude any folder one by one:

    /**
    !/**/
    !/a/aa/aaa/**
    !/b/bb/bbb/**