Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitignore: Ignore all files in folder hierarchy except one specific filetype

Tags:

git

gitignore

I'd like to ignore all files below and in a folder except a specific filetype that could be somewhere in the folders hierarchy:

Example

/Test /Test/unknown/folder/structure/below 

Now I'd like to ignore all files in and below the Test folder except a certain css file named layout.css, e.g.:

/Test/layout.css /Test/fileto.ignore /Test/another/folder/ig.nore /Test/in/a/unknown/folder/layout.css /Test/in/a/unknown/folder/ignore.me 

.gitignore should ignore

/Test/fileto.ignore /Test/another/folder/ig.nore /Test/in/a/unknown/folder/ignore.me 

My .gitignore file does not work:

Test/ !layout.css 

Any suggestions?

like image 272
Jörg Avatar asked Oct 18 '11 07:10

Jörg


People also ask

How do I ignore a specific file type in git?

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.

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.

Can Gitignore ignore a 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 .

How do you exclude or ignore some files from git commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.


1 Answers

I was able to get your example to work by putting a .gitignore file in the Test/ directory with the following contents.

* !*/ !.gitignore !layout.css 
like image 68
David Alber Avatar answered Oct 15 '22 13:10

David Alber