3

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/*
Pawel
  • 73
  • 1
  • 10

3 Answers3

4

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
Pawel
  • 73
  • 1
  • 10
2

Your first assumption **/sim_case is wrong.

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

Literally, you do not want to ignore the full directory sim_case, but just the content of sim_case, except the files in the include directory.

git status proposes to add the sim_case directory. Do so, then :

$ git status
On branch master

Initial commit

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

        new file:   sim_case/include/.gitignore
        new file:   sim_case/include/file1
        new file:   sim_case/include/file2

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        file1
        file2
Dunatotatos
  • 1,706
  • 15
  • 25
2
test/sim_case/*
!test/sim_case/include

Ignoring a directory summarily ignores everything in that directory, and git scans ignore entries in reverse and takes the first match, so the last two entries say "ignore everything in test/sim_case (but still scan the directory) except test/sim_case/include".

jthill
  • 55,082
  • 5
  • 77
  • 137
  • 1
    That will change with 2.8 though. http://stackoverflow.com/a/20652768/6309. `test/sim_case` followed by `!test/sim_case/include` will be enough. – VonC Feb 29 '16 at 10:04