Errol Green
Errol Green

Reputation: 1387

Git ignore except specific subdirectories

I've looked at some examples on how to ignore everything except certain sub directories like so.

# Ignore Everything
foo/*
# Include sub directory
!foo/ccc/*

I have also tried:

# Ignore Everything
foo/*
# Include sub directory
!foo/ccc/

UPDATE

So the code above works, however trying to exclude a directory within another directory within another does not work. It only works on the parent directory and sub directory.

# Ignore Everything
foo/*
# Include sub directory
!foo/ccc/aaa/ddd/ 

After I include this in my gitingore, I run a git add --all and I dont see any the files in git status.

All help would be appreciated.

Upvotes: 1

Views: 83

Answers (1)

T0xicCode
T0xicCode

Reputation: 4951

You have to unignore every directory in the path that you wish to unignore. Something like the following should work in your case:

# Ignore everything in foo
foo/*
# Except the ccc subdir
!foo/ccc

# Ignore everything in ccc subdir
foo/ccc/*
# Except the aaa subsubdir
!foo/ccc/aaa

# Ignore everything in aaa subsubdir
foo/ccc/aaa/*
# Except the ddd subsubsubdir
!foo/ccc/aaa/ddd

It's important that the ignore rules end in /*, as that tells git to ignore everything in the folder, but not the folder itself, permitting us to add exceptions to the ignore rules for specific subdirectories.

Upvotes: 2

Related Questions