Search code examples
gitgitignore

Exclamation mark in .gitignore doesn't work


I have this file structure

aaafolder
└─ bbbfolder
   ├─ filename0.txt
   ├─ dddfolder
   │  ├─ filename1.txt
   │  └─ filename2.txt
   ├─ cccfolder
   └─ filename.txt

I want all the contents of the aaafolder folder to be hidden, but all files in the dddfolder folder must be present.

So in the repository, I want to get in Git this:

aaafolder
└─ bbbfolder
   └─ dddfolder
      ├─ filename1.txt
      └─ filename2.txt

My .gitignore looks like this:

/source/aaafolder/**
!/source/aaafolder/bbbfolder/dddfolder/*

However, the entire aaafolder folder is ignored


Solution

  • Per the gitignore documentation, when negating:

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

    This means that you'll need to ensure that the parent of dddfolder is included, even if the contents of dddfolder are not included.

    In other words, you cannot use the ** to greedily match. You'll need to use a single-level wildcard. You'll need to exclude folders leading up to dddfolder and then include their contents.

    aaafolder/*
    !aaafolder/bbbfolder
    aaafolder/bbbfolder/*
    !aaafolder/bbbfolder/dddfolder