Search code examples
gitgitignore

Git exclude directories with specific name except one


I've seen a few questions that resemble this one and also read the gitignore manual, but still can't figure this one out.

I want to exclude all folders named lib (the rules I have is "lib/") except for a single folder (maybe more in the future) that is a 3rd party folder which I can't change its name, which is under <root>/3rdparty/projectX/lib/.

I've tried this:

!lib/
lib/*
!projectX/lib/

but this also includes other folders with lib folders that are not under root

Is it possible to add this folder as an exception? how?


Solution

  • You have a few options.

    Full path:

    lib/
    !3rdparty/projectX/lib/
    

    Wildcard match:

    lib/
    !*/projectX/lib/
    

    Wildcard match with any level of subdirectories:

    lib/
    !**/projectX/lib/
    

    Your gitignore was not working because it specifically ignores the projectX directory only in the root.