23

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

simhumileco
  • 31,877
  • 16
  • 137
  • 115
mrjbom2
  • 345
  • 3
  • 10

3 Answers3

34

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
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
5

Try it like this :

aaafolder/*
!aaafolder/bbbfolder/*
Antoine Kurka
  • 322
  • 3
  • 9
0

You also need to unignore every single subdirectory separately — bbbfolder/ and bbbfolder/dddfolder/

/source/aaafolder/
!/source/aaafolder/bbbfolder/
!/source/aaafolder/bbbfolder/dddfolder/
!/source/aaafolder/bbbfolder/dddfolder/*
phd
  • 82,685
  • 13
  • 120
  • 165