Question

but this is really weird i was just typing base on what I read here Gitignore Docs

this my weirdest solution:

#ignore everything
*
#except
!.gitignore
!directory**/**

actually this really works!, everything subfolders to what i really want is being seen using git status

NOTE: I have not seen this in the docs or I was blinded by confusion and didnt see it in the docs

Is there anyone who can let me understand why this works? I really want to learn git.

EDIT : same solution to this one by lanzz:

#ignore everything
*
#except
!demo
!demo/**

EDIT Thanks, David

Was it helpful?

Solution

This seems like an obscure git bug to me — !directory**/** is an invalid usage of consecutive asterisks as per the manual, but it doesn't state how such invalid rules are handled.

The correct rules should be:

*
!directory/
!directory/**

The reason is that * matches all files regardless of the directory they're in (because it does not contain a slash), so !directory/ will only allow the directory itself, but not its contents — you need to add !directory/** to match them as well. Using just !directory/** without !directory/ won't work, as !directory/** does not match the directory itself and it will remain ignored.

Edited: !directory/ is preferrable to !directory (without a trailing slash), as !directory will also match files and directories named directory deeper in the directory tree; this will not affect your case, but might be a gotcha in more complex .gitignore rulesets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top