gitignore, how to exclude a specific directory

Whitewind617 picture Whitewind617 · Sep 7, 2016 · Viewed 18.2k times · Source

I'm trying to utilize a .gitignore file to exclude a specific directory, but I'm having some trouble. I looked up how to do this previously on a few forums, including stackoverflow, and while I found it a bit confusing, I had thought I understood it, but it seems to not be working as I thought.

So I have a repo here, and there's a specific directory in the top level called "images" that I want to exclude. Here's what I put in my .gitignore file:

images/

I added and committed everything, all well and good, it ignored the images director in the top level. But then I realized it also ignored forums/images. So what's the deal? How do I handle ignoring a specific directory, but not any others with the same name?

Answer

gview picture gview · Sep 7, 2016

You want to set the root to / which will be relative to the location of your gitignore file.

So you want:

/images

That will ignore any file OR directory named images in the same directory as the gitignore file you are using.

If you want to specify that it should only match the images directory then add the trailing slash:

/images/