stackoverflowuser
stackoverflowuser

Reputation: 1197

gitignore: Ignoring a folder (anywhere) except for a subfolder (anywhere)

I want to exclude everything in a folder named foo, except for stuff inside a folder named bar.

foo can appear anywhere in the directory structure more than once.

bar can appear anywhere in the directory structure under foo.

I tried

/**/foo/**
!/**/foo/**/bar/**

but it didn't work. It still ignores all the foo. It refuses to allow bar.

How do I solve this?

Upvotes: 3

Views: 985

Answers (2)

Alex Wolf
Alex Wolf

Reputation: 20158

From the gitignore documentation:

[A line starting with] An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.

It seems like this isn't possible.

Upvotes: 0

jthill
jthill

Reputation: 60547

When you ignore a directory, you tell git to not even look there.

**/foo/**

ignores everything in foo/, including all the directories. git will never even see a bar directory nested farther down because the above told it to not examine anything in foo

**/foo/**
!**/foo/**/
!**/foo/**/bar/**

will do it, because you overrode the ~don't look~ instruction with ~but do look in all the directories~.

When this alternative makes sense to you, you understand everything:

**/foo/**
!**/foo/**/bar/**
!*/

Upvotes: 2

Related Questions