Drew Angell
Drew Angell

Reputation: 26056

How to ignore all but certain files/directories with Git..?

I see similar questions, but I'm not finding anything that will do what I'm trying to do for some reason.

I have a CodeIgniter PHP library that I maintain, so my project is a full CI install that includes my library. I'm trying to ignore everything except the files that make up my library.

I feel like I've read about the syntax to do this, but I'm getting unexpected results with directories.

First, I start with this:

*
!.gitignore
!README.rst
!instructions.html

You can see here that everything is grayed out (ignored) except for those 3 files under the site root, which means those files are no longer ignored...exactly what I want so far.

enter image description here

When I move on to try and un-ignore the rest of the library files, though, which live in directories, it simply doesn't work. For example, if I add...

!application/controllers/paypal

You can see that the directory and files in it are still grayed out (ignored), and of course when I run git commands they are indeed ignored.

enter image description here

Any information on what I need to do to get this working would be greatly appreciated.

Upvotes: 2

Views: 450

Answers (1)

VonC
VonC

Reputation: 1324737

Simply white-list the folders first.

Then you can start white-listing files

*
!**/
!.gitignore
!README.rst
!instructions.html
!application/controllers/paypal/**

As usual (I mentioned it in "How do I add files without dots in them (all extension-less files) to the gitignore file?", there is mainly one rule to remember with .gitignore:

It is not possible to re-include a file if a parent directory of that file is excluded.

That means, when you exclude everything ('*'), you have to white-list folders, before being able to white-list files.

Check if this is working with git check-ignore -v -- afile to see if it is ignored (and by which rule) or not.

Upvotes: 2

Related Questions