Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore ignoring whitelisted folder

Tags:

git

gitignore

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

like image 409
John K Avatar asked Jul 28 '14 20:07

John K


People also ask

Can Gitignore ignore folder?

Inside . gitignore , you can tell Git to ignore only a single file or a single folder by mentioning the name or pattern of that specific file or folder. You can also tell Git to ignore multiple files or folders using the same method.

Does Gitignore work in folder?

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple . gitignore files.

Should .gitignore be ignored?

gitignore file, even though it's a best practice to use one to designate files you don't want Git to track in version control. Because . gitignore can boost your code quality, you should not ignore .


1 Answers

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.

like image 163
Jason S Avatar answered Oct 01 '22 05:10

Jason S