Ignore a directory except specific files in a subdirectory in Git

So, I’ve tried this with several approaches, but only this one works:


What I want is to ignore all files, except inside /dev/host/mail/htdocs/install/.
So all other files should be ignored entirely, except the ones inside the mentioned sub-folder.

I’ve tried this, but it doesn’t work:


Any idea how to shrink down the mess above? Do I really need as many lines as above; two lines for every directory level?

Am I missing something? Thanks a lot!

  • Running GitLab from Apache subdirectory
  • How do you merge two Git repositories when one is a subdirectory of the other without losing commit history?
  • Configure git to track only one file extension
  • github: how to make it so that the clone project tree is one level deeper?
  • GitIgnore ! File in Subdirectories
  • gitweb snapshot of sub-directory
  • SVN move single directory into other repository (with history)
  • SVN: Is it possible to get the list of revision numbers for given path?
  • One Solution collect form web for “Ignore a directory except specific files in a subdirectory in Git”

    Allowing everything under install after ignoring everything else (*) has the form


    But this also allows stray files at higher levels of the tree. That means adding the additional ignore rules from your question.

    The short answer is what you have is necessary. It is an odd repository structure, so git does not optimize for it.

    Git Baby is a git and github fan, let's start git clone.