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?

Community
  • 1
  • 1
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • Which version of Git are you using? It works for me. – Jonathan Wakely Jul 04 '16 at 12:09
  • `git version 2.7.4 (Apple Git-66)` – mikemaccana Jul 04 '16 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. – mikemaccana Jul 04 '16 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,. – Jonathan Wakely Jul 04 '16 at 12:42
  • @JonathanWakely But you're on 94K - you could get a mug and some stickers! http://meta.stackoverflow.com/questions/291791/what-do-i-get-with-100k-reputation – mikemaccana Jul 04 '16 at 12:45

2 Answers2

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.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
-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*
blue112
  • 52,634
  • 3
  • 45
  • 54
  • 1
    But the OP says _"And modules isn't mentioned in project/.gitignore at all. "_ – Jonathan Wakely Jul 04 '16 at 12:08
  • He ignores /*. So git doesn't scan it see what's inside, and doesn't designore files inside. – blue112 Jul 04 '16 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.**"_ – Jonathan Wakely Jul 04 '16 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 04 '16 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. – Jonathan Wakely Jul 04 '16 at 12:15
  • Thanks @JonathanWakely - indeed, using full paths doesn't change anything. – mikemaccana Jul 04 '16 at 12:31