1

I have the following .gitignore:

# Ignore everything
/*
!/.gitignore

# Readd folders:
#!/mainfolder/ # this works!
!/mainfolder/subfolder/
!/mainfolder/subfolder/*

I want to ignore everything, but subfolder/. Adding the line !/mainfolder/ works, so that /mainfolder is not ignored anymore. But I want to add only the things below subfolder/ and the second and third lines for subfolder/ do not work.

I have found several links online suggesting this is the correct way (e.g. this one). So what am I doing wrong?!

EDIT: I am using Git 2.17.1 from SourceTree on Windows 10. I have tested these results from the integrated MINGW64 bash.

packoman
  • 1,230
  • 1
  • 16
  • 36
  • Possible duplicate of [.gitignore exclude folder but include specific subfolder](https://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder) – phd Jun 18 '18 at 14:56

1 Answers1

2

You need to whitelist folders first, then files:

*
!.gitignore
!/mainfolder/
/mainfolder/*
!/mainfolder/subfolder/
!/mainfolder/subfolder/**

For any file still ignored, check why with:

git check-ignore -v -- path/to/ignored/file

would it be possible to extend the answer on how you would do this for a deeper subfolder? E.g. how would I ignore everything except ´deepsubfolder´(?): !/mainfolder/subfolder/deepsubfolde

That is similar to "Git - Unignore any folder located anywhere inside repository that recursively allows everything":

*
!.gitignore
!*/
!**/deepsubfolder/**
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This works. I know it was not part of the original question, but would it be possible to extend the answer on how you would do this for a deeper subfolder? E.g. how would I ignore everything except ´deepsubfolder´(?): !/mainfolder/subfolder/deepsubfolder – packoman Jun 16 '18 at 09:21
  • Place ** before deep: sub/**/deep/** – VonC Jun 16 '18 at 09:25
  • So this: !/mainfolder/subfolder/**/deepsubfolder/ !/mainfolder/subfolder/**/deepsubfolder/** instead of this(?): !/mainfolder/subfolder/ !/mainfolder/subfolder/** Because that did not work. – packoman Jun 16 '18 at 09:32
  • You missed the trailing /: when the last element is a folder, always add the / – VonC Jun 16 '18 at 09:34
  • Sorry. I accidentally sent the comment prematurely and edited it while you answered. Could you briefly look over it again. The trailing / does not appear to help. Sorry for bothering you. – packoman Jun 16 '18 at 09:38
  • I am on my phone, I will look at it when I go back – VonC Jun 16 '18 at 09:51
  • In the meantime, have a look at my old answer https://stackoverflow.com/questions/50357571/git-unignore-any-folder-located-anywhere-inside-repository-that-recursively-al/50398712#50398712 – VonC Jun 16 '18 at 09:53