.gitignore ignoring whitelisted folder

2019-02-18 15:26发布

I have a folder that shouldn't be ignored according my .gitignore, but git is still ignoring it. I cannot find any other .gitignore file or git configuration that would suggest this folder be ignored, and git check-ignore is printing nothing.

My .gitignore is organized like a whitelist:

*
!.gitignore
!NotIgnoredFolder/
!SomeOtherFolderInRoot

I have a folder along the lines of NotIgnoredFolder/subfolder/js, that git is ignoring. Based on its location and my .gitignore, this path obviously shouldn't be ignored. When I attempt to add it with git add NotIgnoredFolder/subfolder/js, I get

The following paths are ignored by one of your .gitignore files:
NotIgnoredFolder/subfolder/js

I have searched my entire C: drive for .gitignore and .gitignore_global files that may interfere, with no luck. I've also checked my .git/config and my .git/info/exclude. Furthermore, when I try git check-ignore NotIgnoredFolder/subfolder/js, Git prints an empty line.

Is there any way to see what .gitignore file is causing the ignore? Or could this be a result of the way I have set up my gitignore?

Solutions that I have tried with no success:

Git is ignoring files that aren't in gitignore

Git is ignoring .idea folder, but that isn't in gitignore

which gitignore rule is ignoring my file

标签: git gitignore
2条回答
\"骚年 ilove
2楼-- · 2019-02-18 16:01

Git tracks files, not directories, but gitignore rules can match files and directories. Try this:

# Ignore by default
*
!.gitignore
# Whitelist the folder
!NotIgnoredFolder/
# Recursively whitelist its contents
!NotIgnoredFolder/**

If you don't whitelist the directory, git won't descend into it, but if you don't whitelist the contents with ** (or * if you specifically DON'T want to recursively whitelist), it will ignore the files inside the directory.

There are other things people do: /* instead of * at the top to only exclude top-level items by default, or !*/ to whitelist all directories (but not necessarily files inside them). Specifically, !*/ would make the line !NotIgnoredFolder/ unnecessary because it would allow descent into any directory.

But I believe the above is the most specific to your case.

查看更多
贼婆χ
3楼-- · 2019-02-18 16:01

Thanks to Jason pointing me in the right direction, I found a configuration that met my needs:

/*
!.gitignore
!RootDirectory/
!OtherRootDirectory/

.DS_Store
*.bak
*.orig
*.class

This is really only different from the original because we put a slash before the initial asterisk, but it changes the logic so that instead of initially ignoring every specific path and then trying to backtrack out of some of those, it will explicitly ignore only top-level directories (but by extension also their contents). This makes it much more manageable to whitelist folders, however, since we don't have to recursively whitelist the contents with !NotIgnoredFolder/**.

I would give the answer to Jason because his worked too, but I noticed a single downside of that format overriding anything in .git/info/exclude because of its deep reaching nature. This format allows contributors to still make custom ignore rules for themselves.

查看更多
登录 后发表回答