2

I have the following directory structure:

/app
/src
    /MyProject
        /FirstProject
            /Controller
                /file-1-1.php
                /file-1-2.php
                /file-1-3.php
            /Resources
                /view-1.html.twig
                /view-2.html.twig
        /SecondProject
            /Controller
                /file-2-1.php
                /file-2-2.php
                /file-2-3.php
            /Resources
                /view-3.html.twig
                /view-4.html.twig
/vendor
/web

I would like to ignore all files in my repository except files in /src/MyProject/SecondProject/Resources. I have tried many ways but without success. GitHub application does not detect a directories that I want. Usually this application does not detect any file or detects all, so I am confused.

I tried:

# IGNORE:
app/
src/
vendor/
web/

# ALLOW:
  # my first attempt:
!src/MyProject/SecondProject/Resources/*
  # second attempt:
!src/MyProject/SecondProject/Resources/**/
  # third attempt:
!src/MyProject/SecondProject/Resources/**/*

I haven't found a solution in an other questions!

mipadi
  • 398,885
  • 90
  • 523
  • 479
ZaquPL
  • 789
  • 2
  • 12
  • 28

1 Answers1

2

This .gitignore will actually work:

/app
/vendor
/web
/src/**/**
!src/**/
!/src/MyProject/SecondProject/Resources/**

mipadi's answer refers to a mechanism introduced in git 2.7.0 and... reversed in 2.7.1!

The only rule left is:

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

Hence the need for white-listing src sub-folders, before white-listing Resources content.

I tested the above with:

> git --version
git version 2.7.1.windows.1
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I used this solution and it works for me: http://stackoverflow.com/a/20652768/3638163 – ZaquPL Feb 11 '16 at 13:25
  • 1
    @ZaquPL that is a solution that I wrote too (and that I reference here), but you don't need `!src/*`: `!/src/**/` is more precise. It white-lists only the folders. – VonC Feb 11 '16 at 13:27
  • I tried your solution but it didn't work. Perhaps I did something wrong, but I believe that your solution works so I marked this answer as accepted. Thank you. – ZaquPL Feb 11 '16 at 23:03
  • What version of git are you using? – VonC Feb 12 '16 at 04:46
  • @ZaquPL I fixed the answer. I missed the correct ignore rule for `src` folder: it is not `/src/` but `/src/**/**`. – VonC Feb 12 '16 at 07:28