2

I'm excluding a folder a and its subfolders, but want to have a specific subfolder b included, where b can be a subfolder somewhere below a, and the exact path to b has not been specified.

My .gitignore contains these lines:

a/**
!a/**/b

however this does not work. Have tried numerous other permutations of the above, including from other posts on stack overflow, but nothing appears to work.

The previous question on stackoverflow deals with the situation where you can explicitly declare the path to the subdirectory. In this question, the situation I'm addressing is where b can be any subfolder underneath a, and not a predetermined path.

Further the earlier solution requires you to "unignore" every parent directory of anything that you want to "unignore". However in this case I'm trying to create a generic rule that allows this subdirectory without bothering about where it is in the directory tree. So the earlier solution does not work/apply in this situation.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Mickey Puri
  • 835
  • 9
  • 18
  • For everyone's convenience, could you add a link to the other question you're talking about? – Oliver Charlesworth Dec 30 '13 at 12:34
  • This question is a duplicate because quite simply there's just *no better* solution, no alternative. You can either do what was posted there, or it cannot be done, no matter how many times you ask the same question. – janos Dec 30 '13 at 12:44
  • @OliCharlesworth Good point, here is a link the other question, which is different as the subfolder has a specific path rather than being generic http://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder – Mickey Puri Dec 30 '13 at 14:40
  • @Janos, the other question as I've already pointed out is different. Without asking how am I ever to know that what I'm asking "cannot be done". From what I've been reading I had thought it possible but wasn't able to get it to work, your's is the first that I've heard that its not possible. – Mickey Puri Dec 30 '13 at 14:43
  • @janos At least by my definitions of the words involved, "no better solution" is not quite the same thing as "duplicate question"... – twalberg Dec 30 '13 at 15:39
  • I think I covered why it isn't possible in http://stackoverflow.com/a/20652768/6309 – VonC Dec 30 '13 at 15:48

2 Answers2

1

If you tell git to ignore a directory, it will completely ignore everything inside that directory. This means git cannot match your exclude because git is simply not looking at it.

The only way to use excludes in a meaningful way is for a single directory, where you ignore everything but some folder like this:

/some/path/*
!/some/path/foo

This will ignore all entries but foo directly under /some/path.

But, most of the time it is much clearer to just explicitly ignore things than using excludes.

michas
  • 25,361
  • 15
  • 76
  • 121
  • Yes, but it doesn't scale to any path, as suggested in http://stackoverflow.com/a/9604395/6309: I just tested it on Windows with git1.8.4 – VonC Dec 30 '13 at 13:23
  • I meant: **it does not work**, at least on Windows with 1.8.4. '`**/*`' will ignore everything, even with a negation rule just after it. – VonC Dec 30 '13 at 13:43
  • And it doesn't work because it is not possible to re-include a file if a parent directory of that file is excluded, as I mentioned before in http://stackoverflow.com/a/20652768/6309. – VonC Dec 30 '13 at 15:49
1

Try adding the files manually (generally this takes precedence over .gitignore-style rules):

git add /path/to/module

You may even want the -N intent to add flag, to suggest you will add them, but not immediately. I often do this for new files I’m not ready to stage yet.


This a copy of an answer posted to a dupe of this question. I am reposting it here for increased visibility—I find it easier not to have a mess of gitignore rules.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38