3

I have the following directory structure:

project
  modules
    some-company-foo
    different-company-bar
    mycompany-a
    mycompany-b
    mycompany-c 

How can I ignore all the files in modules, except those that start with mycompany?

Similar to git ignore all except subfolder, but using a wildcard. I don't want to have to maintain a list of every mycompany-... folder in .gitignore.

Based on other answers, I've tried creating modules/.gitignore with the following contents:

/*
# Do not ignore mycompany modules
!/mycompany*

And modules isn't mentioned in project/.gitignore at all.

But this doesn't work. Eg,

git add /Users/mike/Documents/project/modules/mycompany-faq/questions/somefile.md

Fails with:

The following paths are ignored by one of your .gitignore files:
modules/mycompany-faq/questions/somefile.md
Use -f if you really want to add them

How can I ignore all the files in modules, except those that start with mycompany?

5
  • Which version of Git are you using? It works for me. Jul 4, 2016 at 12:09
  • git version 2.7.4 (Apple Git-66) Jul 4, 2016 at 12:26
  • @JonathanWakely I've figured out the answer thanks to your test - do you want to paste 'this works as is' so you get the points? I'll add what was causing the problem. Jul 4, 2016 at 12:33
  • 1
    I've got enough rep, glad you figured it out. N.B. that paste.fedoraproject.org link will expire at some point, copy the content into your answer if you want to preserve the info,. Jul 4, 2016 at 12:42
  • @JonathanWakely But you're on 94K - you could get a mug and some stickers! meta.stackoverflow.com/questions/291791/… 😃 Jul 4, 2016 at 12:45

2 Answers 2

1

@jonathanwakely Add your own answer and I'll give you the points instead!

The config was actually correct - thanks to @jonathanwakely's demo, which worked in a new folder.

The problem was that a higher level .gitignore file - above the top of the project, was ignoring the modules directory.

I'd looked for ~/Documents/.gitignore and ~/.gitignore previously, but there's another .gitignore with a different name. I was able to find it with:

> git check-ignore -v modules/mycompany-faq/questions/ssl-or-https.md

/Users/mike/.gitignore_global:28:modules    modules/mycompany-foo/somefile.md

I.e., .gitignore_global was set to ignore the modules directory.

-1

On the .gitignore manual

It is not possible to re-include a file if a parent directory of that file is excluded.

So you need to specify fullpath, like

!/Users/mike/Documents/project/modules/mycompany*
6
  • 1
    But the OP says "And modules isn't mentioned in project/.gitignore at all. " Jul 4, 2016 at 12:08
  • He ignores /*. So git doesn't scan it see what's inside, and doesn't designore files inside.
    – blue112
    Jul 4, 2016 at 12:09
  • 1
    That's in the modules sub-directory, and the manual you linked to says: "An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again." Jul 4, 2016 at 12:11
  • The parent is ignored before, so git will not see what's inside, and cannot file the modules/ directory. Please read my answer again.
    – blue112
    Jul 4, 2016 at 12:14
  • I've read it. It's wrong. I've just tested what the OP claims to be doing, and it works. The modules directory is not excluded, read the question again. Jul 4, 2016 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.