Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitignore, how to exclude a specific directory

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?

like image 932
Whitewind617 Avatar asked Sep 07 '16 20:09

Whitewind617


People also ask

How do I ignore a specific folder in Git?

Inside .gitignore, you can tell Git to ignore only a single file or a single folder by mentioning the name or pattern of that specific file or folder. You can also tell Git to ignore multiple files or folders using the same method. Typically, a .gitignore file gets placed in the root directory of the repository.

Where is the gitignore file located?

For anyone working with git, they would be familiar with the .gitignore file located at the base folder. GIT uses this file to decide, which files/folders to exclude from the source-control. At the top level, there is a .gitignore file, index.html & style.css

How do I exclude a file from a Git project?

In order to exclude that file from your git project, you could create a file called .gitignoreinside your project's directory: touch.gitignore Then inside that file, all that you need to add is the name of the file that you want to ignore, so the content of the .gitignorefile would look like this:

Why can’t i re-include a directory that is excluded from GitHub?

Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. [^1] Uh-oh! The directory github.com/ is excluded. Therefore, it is not possible to re-include a file github.com/realguess (directory is a file).


2 Answers

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/
like image 53
gview Avatar answered Sep 22 '22 03:09

gview


Try changing images/ to /images/ per the .gitignore docs

A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

like image 31
scniro Avatar answered Sep 26 '22 03:09

scniro