I'd like to ignore all files below and in a folder except a specific filetype that could be somewhere in the folders hierarchy:
Example
/Test
/Test/unknown/folder/structure/below
Now I'd like to ignore all files in and below the Test
folder except a certain css file named layout.css
, e.g.:
/Test/layout.css
/Test/fileto.ignore
/Test/another/folder/ig.nore
/Test/in/a/unknown/folder/layout.css
/Test/in/a/unknown/folder/ignore.me
.gitignore should ignore
/Test/fileto.ignore
/Test/another/folder/ig.nore
/Test/in/a/unknown/folder/ignore.me
My .gitignore file does not work:
Test/
!layout.css
Any suggestions?
Quite old question but since I was struggling with this issue even now I figured I would share the actual solution to the problem.
The thing is while you cannot commit empty directories in Git as if they were ignored, this rule does not apply to
.gitignore
.From https://git-scm.com/docs/gitignore
** Matches everything inside - literaly everything - files AND directories.
This leads us to another point in gitignore docs:
Now, let's say we have a following dir structure:
And we want to ignore all files inside cache/ except for README files.
If we specify gitignore like this:
Git will ignore everything except for
/cache/README
. The other README won't show up because its directory/cache/dir
was excluded with/cache/**
and!README
won't even apply to it. To solve the issue we need to specify gitignore as this:I was able to get your example to work by putting a
.gitignore
file in theTest/
directory with the following contents.In order to accomplish what you want, you'll need to use some negative exclusions.
The basic idea is that you need to exclude every parent directory of any file that you want unignored.
So, if you want /Test/in/a/unknown/folder/layout.css to be added to your git repo, then you'll have to unignore /Test/, /Test/in/, /Test/in/a/, /Test/in/a/unknown/, and /Test/in/a/unknown/folder/.
Then, when you finally get to the directory with some ignored and some unignored files, you'll need to specify each individually as follows:
So when you run
$ git add-all
you'll see your desired results:Note: You can find an explanation of why
git add-all
is the best way to add files to a git repo at http://lexsheehan.blogspot.com/2014/05/git-add-update.html