Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whitelisting and subdirectories in Git

Tags:

git

gitignore

I have created a white-list for text files only.

* !*.txt 

Now, I have an untracked text file in a sub-directory - sub/dir/file.txt, and this is NOT shown (it is ignored). Text files in the root directory are shown, however.

Why is that, and how do I fix it?

like image 794
Nate Avatar asked Feb 06 '12 15:02

Nate


People also ask

What is a subdirectory in git?

git. Git uses this special subdirectory to store all the information about the project, including all files and sub-directories located within the project's directory. If we ever delete the . git subdirectory, we will lose the project's history. Next, we will change the default branch to be called main .

Does .gitignore work in subdirectories?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

Does git add include subdirectories?

It will add all the Folders , Subfolders and files to the existing repo.

How do I set Git to ignore all text files?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.


1 Answers

If you try it that way, it'll fail, because you'll end up blacklisting the directories in your structure.

To solve, you want to blacklist everything that is not a directory, and is not one of the file-types you want to commit, while not blacklisting directories.

The .gitignore file that will do this:

# First, ignore everything * # Now, whitelist anything that's a directory !*/ # And all the file types you're interested in. !*.one !*.two !*.etc 

Tested this in a three-level structure white-listing for .txt files in the presence of *.one, *.two and *.three files using a .gitignore located in the root directory of the repository - works for me. You won't have to add .gitignore files to all directories in your structure.

Information I used to figure out the answer came from, amongst other things, this (stackoverflow.com).

like image 50
simont Avatar answered Sep 18 '22 09:09

simont