.gitignore whitelist on directory and its contents

gitignore whitelist not working
gitignore all except
gitignore except folder
gitignore all except extension
gitignore hierarchy
man gitignore
gitignore specification
.gitignore directory recursive

I'm trying to whitelist the directory (and its contents) SupplierName in my Zend Framework 2 vendor directory.

The original .gitignore file in /vendor looks like this:

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*

Now I'd like to whitelist the directory SupplierName which shouldn't be too hard I thought. I have read the docs on gitignore and tried the following configurations:

First try, add !SupplierName right after the comment which says that I have to add the whitelisted path here.

# Add here the vendor path to be whitelisted
!SupplierName
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*

Right after that I executed git status which didn't show the vendor/SupplierName directory. git add vendor/SupplierName showed the following message:

The following paths are ignored by one of your .gitignore files: vendor/SupplierName

Second try

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!SupplierName
!.gitignore
*

Right after that I executed git status which didn't show the vendor/SupplierName directory. git add vendor/SupplierName showed the following message:

The following paths are ignored by one of your .gitignore files: vendor/SupplierName

Third try

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
!SupplierName

Right after that I executed git status which didn't show the vendor/SupplierName directory. git add vendor/SupplierName seems to work. But now, when I want to add the Module.php file (and some other files, subdirectories, etc) the following happens. git add vendor/SupplierName/Module.php -->

The following paths are ignored by one of your .gitignore files: vendor/SupplierName/Module.php

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
*
!.gitignore
!SupplierName
!SupplierName/
!SupplierName/*

Allows me to add files directly in vendor/SupplierName, but git add vendor/SupplierName/config/module.config.php still results in

The following paths are ignored by one of your .gitignore files: vendor/SupplierName/config/module.config.php

I've been searching for problems regarding recursive whitelisting, because that seems to be the problem, but nothing came up.


You can use 2 .gitignore files to achieve the desired result:

# vendor/.gitignore
*
!.gitignore
!SupplierName/
!SupplierName/*

# vendor/SupplierName/.gitignore
!*

I tested this with a test repo and seems to work for me in adding files as many levels deep underneath the vendor/SupplierName directory.

$ git add .

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   vendor/.gitignore
#   new file:   vendor/SupplierName/.gitignore
#   new file:   vendor/SupplierName/a
#   new file:   vendor/SupplierName/b
#   new file:   vendor/SupplierName/c
#   new file:   vendor/SupplierName/d
#   new file:   vendor/SupplierName/dir1/d
#   new file:   vendor/SupplierName/dir1/dir4/dir5/dir6/dir7/dir8/dir9/dir10/somefile
#   new file:   vendor/SupplierName/dir1/dir4/f1
#   new file:   vendor/SupplierName/dir1/dir4/f2
#   new file:   vendor/SupplierName/dir1/dir4/f3
#   new file:   vendor/SupplierName/dir1/dir4/f4
#   new file:   vendor/SupplierName/dir1/e
#   new file:   vendor/SupplierName/dir1/f
#   new file:   vendor/SupplierName/dir3/dir6/f5
#   new file:   vendor/SupplierName/dir3/dir6/f6
#   new file:   vendor/SupplierName/dir3/dir6/f7
#   new file:   vendor/SupplierName/dir3/dir7/f8
#   new file:   vendor/SupplierName/e
#

Effective .gitignore whitelisting patterns, The !*/ rule un-ignores all directories. But Git does not track directories, only files, so !*/ by itself will only allow descent into the full directory tree;  Whitelisting with .gitignoreis a technique for dealing with source trees that can have various different untracked local files such as generated output, packages installed through package managers, “working” files, config for individual developers, etc.


You can also achieve this with only one .gitignore file (in your project root):

/*
!/.gitignore
!/vendor
/vendor/*
!/vendor/SupplierName

Whitelisting subdirectories using Git's .gitignore, Here's how to whitelist a subdirectory multiple directories deep in your file-​structure, keeping all other files and directories blacklisted. Every directory between the repository root and the subdirectory that should be tracked must be whitelisted separately, without recursion, and with trailing slashes, plus an additional recursive path to that subdirectory, in order to whitelist its contents. Here’s what that looks like: # contents of .gitignore -- works * # ignore everything


Found an interesting article: https://jasonstitt.com/gitignore-whitelisting-patterns

All credits to Jason Stitt. Texts are copied from the site above:

Ignore everything, then add specific subtrees
# Ignore everything
*
# But descend into directories
!*/
# Recursively allow files under subtree
!/subtree/**
# You can be specific with these rules
!/some/other/deep/path/**
!.gitignore 

The !*/ rule un-ignores all directories. But Git does not track directories, only files, so !*/ by itself will only allow descent into the full directory tree; it won’t actually allow anything into the repo. With that rule in place, you only need one rule using the ** recursive wildcard in order to include a subtree.

If you didn’t use !*/, you would need additional rules to un-ignore /subtree/ and its child directories.

Not everyone likes !*/ because it means that if any other rule allows a filename pattern found inside some directory you don’t want in the repo, the directory itself will not be blocked. You need to use specific rules for files to include with this one.

Ignore the root directory, then add whole subtrees
# Ignore everything in the root
/*
# Un-ignore all of subtree
!/subtree/
!.gitignore 

This pattern is somewhat coarser than the previous one. The /* rule will only ignore items in the root of the repo’s directory structure, so as soon as you whitelist a directory, all of the directory’s contents will be allowed as well, even without using the * or ** wildcards.

Ignore everything in a directory, but keep the empty directory
*
!.gitignore

Git does not want to include an empty directory in a repo, because it tracks files. Put a hidden file (such as .gitignore) into the directory, and it will be saved. But to keep the directory empty, even if you have files in there for testing/development purposes, it’s a good idea to ignore everything except for the .gitignore file itself.

Gitignore whitelist approach #git · GitHub, whack-a-mole! This gitignore approach excludes everything except for what we want to track. Blacklist files/folders in same directory as the .gitignore file. /**. The .gitignore file allows you to exclude files from being checked into the repository. The file contains globbing patterns that describe which files and directories should be ignored. gitignore.io is an online service that allows you to generate .gitignore files for your operating system, programming language, or IDE.


I had a similar issue when moving from CVS to Git.

Unlike CVS, Git doesn't look at directories, it focuses on files.

For example you can't not-ignore directory "a" but you can not-ignore all files in directory "a" like so: !a/*

The same is true for subdirectories.

If directory "a" has a subdirectory "b" and you ignore "!a/*" then you will still get all files in "a/b".

So you then have to ignore that too "!a/b/*" and so on for all subdirectories that you want to white list.

You only need one .gitignore file.

so you end up with something like:

# ignore everything
*
# except for .gitignore files in the current directory
!.gitignore
# and all files in directory a
!a/*
#and all files in subdirectory b
!a/b/*

With this you would still get files from a/c and a/b/c. I'm not sure if there is a workaround for recursion down subdirectories.

whitelist gitignore · GitHub, http://kernel.org/pub/software/scm/git/docs/gitignore.html. # Ignore everything in the root except the "wp-content" directory. .DS_Store .svn .cvs. *.bak. *.swp. Patterns inside the .gitignore file are matched from the root directory of the git repository. Patterns are comprised of a wildcard character *, to match any character, and literal characters to match the exact phrase. A typical example of using a .gitignore file would be to exclude all files ending in .log.


You should include everything in the blacklist first, then make every directory and subdirctory in the whitelist. For example, I only want to put DIR /opt/source/, DIR /opt/nginx/ and FILE /home/test/.opt/hello.txt in whitelist, could write .gitignore file like this to make it work:

/*
!/.gitignore
!/opt
/opt/*
!/opt/source/
!/opt/nginx/
!/home
/home/*
!/home/test
/home/test/*
!/home/test/.opt
/home/test/.opt/*
!/home/test/.opt/hello.txt

How can I whitelist a file or directory that is in my .gitignore?, For instance, to override the .gitignore in the question for the files in the /dev folder, you could use a .projectile file that looks like this: !/dev/. ** these explanations assume your .gitignore file is in the top level directory of your repository, as is the convention. If your repository has multiple .gitignore files, simply mentally replace "repository root" with "directory containing the .gitignore file" (and consider unifying them, for the sanity of your team).*


Ignore files, gitignore and by default is a blacklist. Docker. Docker images are typically built from the contents of a directory using the docker build command. In software projects, .gitignore typically contains a listing of files and/or directories that are generated during the build process or at runtime. Entries in the .gitignore file may include names or paths pointing to: temporary resources e.g. caches, log files, compiled code, etc.


Help on proper use of .gitignore: ignore everything except some files , Git tracks files, not directories, but gitignore rules can match files and directories. Try this: # Ignore by default * !.gitignore # Whitelist the folder ! If a filename or pattern in your .gitignore file happens to match the name of a directory, that directory and all of its contents will be ignored. If all you only want to ignore directories by that name, use. name/. To ignore anything, directory or file, at the top level of your working tree, use. /name.


How to use your .dockerignore as a whitelist, The folder structure is the following: Finally, how is the regexp support on git? Can I use something like !\.c$ in my .gitignore to not ignore all files that ends with​  Patterns read from the command line for those commands that support them. Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file.