Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I add something to the .gitignore so that the match is not recursive?

How to I add something to the .gitignore so that the match is not recursive?

For example, I wish to ignore the directory foo and the file bar.txt in the current directory, but not any that exist in subdirectories.

I have tried this for my .gitignore file:

foo/ bar.txt 

But unfortunately git applies this recursively, so that otherdir/bar.txt and otherdir/foo/ also get ignored, which is not what I want.

(Is there a command in git that shows me all ignored files, and reference the .gitignore file that is responsible for the file being ignored? This would be useful for debugging.)

like image 721
pauldoo Avatar asked Mar 19 '10 11:03

pauldoo


People also ask

How do I add something to Gitignore?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Does Gitignore ignore subdirectories?

If the pattern doesn't start with a slash, it matches files and directories in any directory or subdirectory. If the pattern ends with a slash, it matches only directories. When a directory is ignored, all of its files and subdirectories are also ignored.

How do I Gitignore everything except?

You want to use /* instead of * or */ in most cases The above code would ignore all files except for . gitignore , README.md , folder/a/file. txt , folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders.


1 Answers

The solution is to place a leading slash on the .gitignore entries:

/foo/ /bar.txt 

(I thought I tried this before posting on StackOverflow, but clearly I hadn't tried it properly, as this works perfectly.)

like image 173
pauldoo Avatar answered Oct 05 '22 13:10

pauldoo