Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore whitelist on directory and its contents

Tags:

git

gitignore

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.

like image 908
ivodvb Avatar asked Mar 08 '13 07:03

ivodvb


People also ask

How do I Gitignore the contents of a folder?

. gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

Can you Gitignore a folder?

A . 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 .

Should .gitignore be added to repository?

Normally yes, . gitignore is useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOG or something. In those cases you probably don't want to force that on anyone else.


3 Answers

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
#
like image 78
Tuxdude Avatar answered Oct 21 '22 09:10

Tuxdude


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

/*
!/.gitignore
!/vendor
/vendor/*
!/vendor/SupplierName
like image 41
Chronial Avatar answered Oct 21 '22 08:10

Chronial


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.

like image 25
Simon Lang Avatar answered Oct 21 '22 10:10

Simon Lang