2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Drew Angell
  • 25,968
  • 5
  • 32
  • 51
  • Can you please share the complete content of your .gitignore file? – Nadeem Khan Jan 29 '16 at 08:01
  • The complete content consists of only the lines shown in the question. It's just the 5 lines right on top of each other. Nothing after the !application/controllers/paypal – Drew Angell Jan 29 '16 at 08:04

1 Answers1

2

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This seems to be giving me the [same result](http://fiber.angelleye.com/forums/stack-overflow/gitignore-3.png). – Drew Angell Jan 29 '16 at 08:33
  • @AndrewAngell What version of git are you using, on which OS? – VonC Jan 29 '16 at 08:34
  • @AndrewAngell and try with `!application/controllers/paypal/*` (one `*` instead of two `**`) – VonC Jan 29 '16 at 08:34
  • git version 2.5.2.windows.2 – Drew Angell Jan 29 '16 at 08:35
  • One * isn't changing anything. – Drew Angell Jan 29 '16 at 08:35
  • @AndrewAngell Try and upgrade to 2.7.0 (https://github.com/git-for-windows/git/releases) to see if the issue persists. – VonC Jan 29 '16 at 08:36
  • @AndrewAngell What a `git check-ignore -v -- application/controllers/paypal/payflow.php` returns? – VonC Jan 29 '16 at 08:37
  • Now I'm running git version 2.7.0.windows.1 and I'm still getting the same result. When I run the check-ignore it just returns nothing, as if I ran a git diff and there were no differences. – Drew Angell Jan 29 '16 at 08:46
  • @AndrewAngell If it returns nothing, it means the file is *not* ignored. – VonC Jan 29 '16 at 08:49
  • @AndrewAngell `git diff` will return something only if the file was added and committed (ie versioned) before. If it is (from a git repo perspective) a brand new file, `git diff` returns nothing. – VonC Jan 29 '16 at 08:51
  • Well, that's weird then because it's not tracking those files that are still grayed out in the screenshots. When I run git status right now the only thing that comes up is the .gitignore. – Drew Angell Jan 29 '16 at 08:53
  • @AndrewAngell if there any `.git` in `application/controllers/paypal/` folder or in any of its parent folder? (checking for a nested git repo here) – VonC Jan 29 '16 at 08:54
  • Ah, doh! I hadn't changed anything about the other files so they simply weren't coming up with git status. The color coding is apparently broken in this PHPStorm plugin. It does seem to be tracking. Thanks! – Drew Angell Jan 29 '16 at 08:54
  • @AndrewAngell Did you relaunch PHPStorm? – VonC Jan 29 '16 at 08:55
  • Yes. The color coding is from a plugin, so I reported the issue to them. – Drew Angell Jan 29 '16 at 09:00