33

I've been looking around and just could not find the same scenario that I have. I have:

--public
      |--img
           |--logo (folder)
           |--post_image (folder)
           |--banner.jpg
           |--icon.ico
           |--image1.jpg
           |-........etc

I need to ignore the logo and post_image folders, but I want to keep every file that is inside img folder. How would I go about it?

Right now I have **/public/img but I feel is not the right approach!

Cristian
  • 2,390
  • 6
  • 27
  • 40

3 Answers3

47

I found this to be simpler (at least in the case there are many subfolders):

public/img/*/

This will exclude all subfolders in public/img/, but will not exclude the files in this folder.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • This did not seem to work for me. added a hidden file to each directory and only want subdirectories ignored, but /folder/subfolder/*/ is still ignoring subfolder. – raw-bin hood Oct 01 '18 at 17:41
  • @rippledj I don't quite understand what you mean. `/folder/subfolder/*/` will not ignore `/folder/subfolder/`, but each directory inside. What do you the hidden files expect to change? – Lorenz Meyer Oct 01 '18 at 18:50
  • So, I think my mistake was that I wanted to keep the empty folders in the repo (diff than OP goal). So, I thought that /folder/subfolder/*/ would keep the folder if I just put an empty hidden file inside. However, it was the same as ignoring all the folder contents. I needed a different solution. (!/folder/.keep). – raw-bin hood Oct 01 '18 at 19:25
8

You can put a .gitignore file into any child directory, it doesn't have to be at the root. In your case, a .gitignore file in the public/img directory with the content:

logo
post_image

would do the job.

For another approaches, you might want to look at:

Community
  • 1
  • 1
reto
  • 9,995
  • 5
  • 53
  • 52
  • Is it also possible to do it from the root directory? Like have a .gitignore file in the root with `/img/logo` and `/img/post-Image`? Is there a difference if I do it from the root directory or straight from the child directories? – Cristian Jan 27 '16 at 16:02
  • 2
    Actually I want to ignore all subfolders but not files. In the OPs that would mean I don't want to specific `logo` and `post_image` specifically because if I add another folder and forget it won't be ignored. Is there a way to specify ignore all subfolders but not files in a specific folder? In other words `dist/foo.txt` yes, `dist/someFolder` no – gman Nov 21 '16 at 14:00
2

Please try below options .

public/img/*

!public/img/logo/

!public/img/post_image/

git-ignore

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
Chandan
  • 117
  • 1
  • 6