Search code examples
gitgitignore

.gitignore, exclude all files in a folder... but keep those sub-folders with a .gitkeep inside it?


I'd like to ignore all files in log folder, except for .gitkeep files (I need those files for keeping empty directories):

- log
  |- foo.log (should be ignored)
  |- folder1 (should be ignored)
  |- folder2 (keep it because contains a .gitkeep file)
     |- .gitkeep

Not working:

/log/*
!/log/*/.gitkeep

Using ** doesn't work too:

/log/*
!/log/**/.gitkeep

Is this possible... without manually exclude each subfolder, like this?

/log/folder1/*
/log/folder2/*
!/log/folder2/.gitkeep

Solution

  • Unignore directories ignored with *:

    /log/**
    !/log/*/
    !/log/*/.gitkeep
    

    Without this git doesn't even look into ignored subdirectories.