.gitignore - Ignore everything in a directory except one file

Viewed 31460

I know there is a lot of questions like this, but no one solved my problem. I want something very simple - ignore all files and folders under specific folder except one file. This is what I try:

#Ignore
public/typings/*

#Allow
!public/typings/browser/ambient/jquery/jquery.d.ts

...but the file is still ignored.

Any suggestions?

3 Answers

I've been struggling a lot with this subject, let me share a procedure that worked for me:

1) Add the top level folder "abc/" to your .gitignore 2) Let's say that in "abc/" you have also "def/" and "jkl/" folders. If you want to keep the contents of "jkl/" you can just do:

git add -f jkl/*.php

Ok! Now everything inside "jkl/" with .php extension will be tracked by git, even in the next commits, without any headaches!

I've found this to be the right solution for my case, because I was really going insane trying to understand how gitignore does scan files and directories.

Related