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

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

For example, I want to ignore the foo directory and the bar.txt file in the current directory, but not everything that exists in the subdirectories.

I tried this for my .gitignore file:

 foo/ bar.txt 

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

(Is there a command in git that shows me all the ignored files and refers to a .gitignore file that is responsible for ignoring the file? That would be useful for debugging.)

+52
git gitignore ignore
Mar 19 '10 at 11:12
source share
2 answers

The solution is to put an upper slash in the .gitignore :

 /foo/ /bar.txt 

(I thought I tried this before posting on StackOverflow, but obviously I didn’t try this correctly, as it works fine.)

+90
Mar 19 '10 at 11:22
source share

From the gitignore man page:

Extra prefix! what denies the pattern; any comparable file excluded by the previous template will be included again. If the matching pattern matches, this will override the sources with lower priority.

So !* , Since the first line in your .gitignore will clear all previous templates.

+11
Mar 19 '10 at 11:24
source share



All Articles