Search code examples
gitgitignore

Unignore specific Files in Subdirectory with .gitignore


I have a problem to get .gitignore to do what I want. My folder structure looks like so:

assets
├── img
|   ├── thousands
|   ├── of
|   ├── folders
|   ├── KEEP_SOMETHING_IN_THIS_FOLDER
|   |   ├── another
|   |   ├── thousands
|   |   ├── of
|   |   ├── folders
|   |   ├── KEEP_THIS_FILE_1.jpg
|   |   ├── KEEP_THIS_FILE_2.jpg
|   |   ├── KEEP_THIS_FILE_3.jpg

I try to keep the three jpgs. I tried

/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg

Solution

  • You need to create .gitignore file in specified folder. In "KEEP_SOMETHING_IN_THIS_FOLDER" in your case. And write this lines:

    /**
    /*.jpg
    !not_ignore.jpg
    

    At first commit this .gitignore file and then try commit your files.
    But if your files already has been staged (tracked), try git rm --cached <filePath> these files.