Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitignore exclude subfolders (with **/ pattern) except specific one

Tags:

git

I was searching for this answer here on SO but nothing was similiar or I got lost. I am trying to exclude all in specific subfolder except one folder. Probably I could do all paths manually, but I think there should be clever way. My file structure:

.gitignore
test/
    file1
    file2
    sim_case/
        file1
        file2
            include/
                file1
                file2

In .gitignore I have:

**/sim_case

And that is excluding everything from sim_case, but I would like to include folder "include". I tried some variations, and nothing worked.

!**/sim_case/include
!**/sim_case/include/
!**/sim_case/include/*
!sim_case/include/
!sim_case/include/
!sim_case/include/*
like image 991
Pawel Avatar asked May 22 '26 19:05

Pawel


1 Answers

I did little research and it seems that the best option to exclude only files inside "sim_case" folder is to use:

**/sim_case/*
!**/sim_case/include

That allows to have more complex file structure

test/sim_case/include
test1/sim_case/include
etc.

The result from "git status":

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .gitignore
    new file:   test/file1
    new file:   test/file2
    new file:   test/sim_case/include/file1
    new file:   test/sim_case/include/file2
    new file:   test1/file1
    new file:   test1/file2
    new file:   test1/sim_case/include/file1
    new file:   test1/sim_case/include/file2
like image 56
Pawel Avatar answered May 25 '26 09:05

Pawel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!