3

Git has the .gitignore file which is for a blacklist on excluded files/folders.

Is there an adversary function to this? Here is my use case.

BigDir/
  DirAA/
  DirAB/
  DirAC/
  DirAD/
  DirAE/
  .....

Is there a way to just say "allow only these directories?" (a whitelist)

Maybe something like...

.gitkeep

DirAA/
DirAC/

which would result in All other directories BUT DirAA and DirAC to be ignored.

I would just add each other directory to the .gitignore but these folders keep growing, and I wouldn't want to keep adding each new folder.

Community
  • 1
  • 1
ddavison
  • 28,221
  • 15
  • 85
  • 110
  • IIRC, `.gitignore` doesn't affect files that are already checked in. You could commit the few files they you want, then add the whole directory to `.gitignore`. – Benjamin Hodgson Jun 02 '14 at 21:56
  • by `whole directory`, that would mean that I would need to add each sub-directory to the `.gitignore`, I don't want this – ddavison Jun 02 '14 at 21:57
  • You can do inverse ignoring in gitignore, basically you ignore everything and then specificy which files you do not want to ignore – Tim Jun 02 '14 at 21:57
  • I haven't tried this personally, but [the documentation](http://git-scm.com/docs/gitignore) says for the pattern format: *An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.* – M.M Jun 02 '14 at 21:57
  • @MattMcNabb I'm familiar with the `!` prefix. I hadn't gotten it to work correctly for me. the [.gitignore](http://hastebin.com/ikilayunid.md) I was playing around with, didn't work when I had a directory, `a`,`b`,`c` and `d` – ddavison Jun 02 '14 at 21:58
  • @MattMcNabb `It is not possible to re-include a file if a parent directory of that file is excluded.` Looks like that's by design. so no, I can't exclude all, then negate `:(` – ddavison Jun 02 '14 at 22:06
  • @sircapsalot If you create a `.gitignore` in `BigDir` and give it `/*` that means to only ignore files in that directory, not recursively (the "root" for paths starting `/` is the location of the gitignore if it's within the tree). So e.g. `/*` `!DirBC/` will ignore everything except `BigDir/DirBC` – M.M Jun 02 '14 at 22:06
  • You can't exclude a directory but the include a particular file under that directory, no. But you can include the directory, then exclude all files in it except the one you want. However your question as asked doesn't seem to involve that situation – M.M Jun 02 '14 at 22:07
  • 1
    I think this [other post from StackOverflow][1] can help. [1]: http://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files – amalbala Jun 03 '14 at 07:42

1 Answers1

6

Per help from @amalbala's comment, this is how I can achieve what i want:

.gitignore

# first ignore everything
*

# then don't ignore the root directory
!*/

# now whitelist the particular directories and all files within them
!DirAA/*
!DirAC/*

This will result in everything being ignored, BUT DirAA/ and DirAC/.

ddavison
  • 28,221
  • 15
  • 85
  • 110