.gitignore to ignore all files, then recursively allows files of a certain type
Asked Answered
C

2

4

I have a folder that is purely a T4 template folder but it outputs code generated files into other directories. So I'm looking to ignore everything except *.tt files (i.e. the *.cs, *.master, *.resx, etc.). There is one gotcha. I have a subfolder called static that contains files that should never be ignored.

/Test.tt
/Test.cs
/TestHtml.tt
/TestHtml.html
/Static
   /Sub/Sub.cs
   /Sub/Sub2/Sub2.cs
   /Sub3/Sub4/Sub5/Sub5.html

I only want to ignore the /Test.cs and /Test.html but include all other files. I've tried the following:

/.gitignore

# Ignore all
*

# But not these files...
!.gitignore
!*.tt

/Static/.gitignore

!*.*
#also have just tried blank

I can't get git to ignore the right things...main problem is the 'recursive exclude' I want for Static/..

Cereus answered 3/5, 2011 at 21:42 Comment(0)
I
12

You need to anchor your * otherwise it will continue to match all files even in un-ignored directories.

e.g.

/*

!.gitignore
!*.tt
!/Static/
Italy answered 3/5, 2011 at 21:48 Comment(13)
I've put this in, however, git status shows .tt (good) and Static/ . I wasn't sure if Static/ meant it would add all files so I did a git add ., followed by git ls-files, but no files in Static/ were added (note, there are no files present in Static/ until at least one level deep - that is Static/*/.* is first 'valid' level, but there are deeper levels too). Any other ideas?Cereus
@Terry: I've just run a test case with this .gitignore and it works for me. Do you have any other .gitignore files other than the one that I describe or anything in your .git/info/exclude or any other custom ignore file?Italy
I don't have any other .gitignore files. I don't have a .git/info/exclude file either. I do have a global .gitignore but it just ignores standard 'Visual Studio Build Files'...(i.e. no mention of *.cs or /Static). Should I get screen shots and ls dumps into dropbox for examination?Cereus
@Terry: What do git ls-files -o and git ls-files -oi --exclude-standard show?Italy
@charles-bailey - -o returns what looks like 'everything' (note, I have not committed/or added to index yet). -oi --exclude-standard seems to only show the files I want to ignore in the root (correct). Is this what you expected? 'Seems' right, except for fact that when I do an add . that the ls-files doesn't list andy Static/*/*.* files.Cereus
@Terry: In that case I don't understand why add didn't work. What version of git are you using?Italy
@charles-bailey - recent ;) How do I check? New to git.Cereus
@charles-bailey - 1.7.4.msysgit.0Cereus
@Terry: What happens if you try !/[Ss]tatic/ instead?Italy
I have a practically identical requirement but this doesn't work. Ignoring the 'Static' folder requirement... Create a new repo (git init), add a .gitignore file as above, create a new folder 'test1', create 2 files in that folder include.tt & dontinclude.foo, do a 'git add .' and a 'git status' & all files are ignored... I tried it with git 1.7.3 & then with 1.7.7 - am I missing something?Utah
@PandaWood: Did you unignore the test1 directory as Static is in the example?Italy
I had to add !*/ to not ignore all subdirectories - otherwise basically nothing is found unless it's in the root directory - #8025424Utah
@PandaWood: That sounds correct, it's what should happen if you are ignoring everything at the top level (i.e. /*). An ignored directory shouldn't be searched by git for new files.Italy
U
0

Something like:

/*
!.gitignore
!*.tt
!/Static/

should work.

Look at my answer below for making it more complex when you want to unignore a subdirectory within some other folder when you are ignoring everything else.

Can't understand how gitignore ignores the folders

Ultramontanism answered 3/5, 2011 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.