1

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?

1
  • You only need .gitkeep files in directories that don't have anything else in them. In your case, the various test.txt files make them redundant.
    – jonrsharpe
    Commented Feb 26, 2017 at 12:48

1 Answer 1

1

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/**
1
  • Thanks a lot! !/**/ is the key. /** and * actually has the same effect.
    – Wang
    Commented Feb 26, 2017 at 13:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.