问题
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.
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/20839642/git-including-an-arbitrarily-nested-subfolder-when-its-parent-has-been-excluded