This question has already been answered:

In one rather old project, in the / public directory there are files that are generated by the project itself, but all other files need to be taken under version control. Files that do not need to be added to Git can be written with this pattern: ^[a-z0-9].php$ (regular for PHP, ^ - beginning of line, $ - end), but at the same time there are several files: index.php , main.php , etc, which need to be monitored, but they also fall into the template ^[a-z0-9].php$ . Please suggest how to write such a script in .gitignore ?

Reported as a duplicate member Nick Volynkin 18 Dec '16 at 5:58 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Look at the solutions from the question. How not to ignore the file in the ignored folder? . There, almost the same problem is solved, but in a more general form - to selectively add files when the folder is already completely added to .gitignore . You, too, may later have new files in /public that will need to be versioned. - Nick Volynkin

2 answers 2

In order to exclude any file from being ignored, it is enough to add a prefix matching mask ! :

 # игнорировать все файлы и поддиректории * # но сохранить сам .gitignore !.gitignore 

The only problem you may encounter is saving files inside a subdirectory of the ignored directory:

 * # не сработает, потому что потомки subdirectory не будут распознаны !subdirectory/file 

Then you have to resort to hacks

 * !subdirectory subdirectory/* !subdirectory/file 
  • Not exactly what was needed, but thanks for the useful information about the subdirectories and wax. sign! - Miron

While I was waiting for an answer, I figured it out myself. First you need to manually add all the necessary files and directories to the index:

 $ git add public/dir1/ $ git add public/dir2/ $ git add public/index.php $ git add public/main.php $ git add public/etc.php 

And then add the line to .gitignore:

 public/*.php 

And everything ... everything turned out to be much simpler than it seemed at first glance.