4

This is the content of my "root" .gitignore

# exclude everything ...
*
# ...except
!/.gitignore
!*/
!/modules/wp-azth/**

the problem is that, under modules folder, i've a lot of third-party modules with a .gitignore file inside.

Using rules above, all third-party modules folders are ignored but their .gitignore files are not ( and i don't need it of course )

is there a way to ignore .gitignore files inside ignored subfolders? maybe .git/config can be used for this?

( i think it's a bad behaviour of git that considers them even if they are ignored )

UPDATE: It seems a lack of git, it allows ignored folder to "arbitrary" un-ignore itself having !.gitignore rule inside a gitignore file placed ( by anyone ) inside an ignored subdirectory. Ignored folders normally could contains files that are dynamic , temporary or 3rd party ( cache, temp, plugins etc ) ... so git allows to create unwanted behaviours just using a simple !.gitignore as rule inside subdirectory.

git gui screenshot

Joseph
  • 1,029
  • 13
  • 26
  • what about `/**/.gitignore` ? (I don't know if it's work, but this is the way i would have tryed) – Anthony Raymond Jan 01 '16 at 02:58
  • Already tried ( put at the end of gitignore rules ) but it doesn't work – Joseph Jan 01 '16 at 03:04
  • If you run `git check-ignore -v -n ` on one of the files what does it say? There's probably an override somewhere in your `gitignore` path. – Guildencrantz Jan 01 '16 at 04:52
  • What version of git are you using? – VonC Jan 01 '16 at 07:09
  • @VonC my git version is 1.9.1 – Joseph Jan 02 '16 at 11:38
  • @Guildencrantz where should i run this? i've tried "git check-ignore -v -n ." on git project root dir but it says: " :: . " , nothing else – Joseph Jan 02 '16 at 11:39
  • @Yehonal: https://git-scm.com/docs/git-check-ignore You need to pass the file you want to check. I suggest you run the command specified _on one of the files that you aren't expecting to see_. – Guildencrantz Jan 02 '16 at 18:26
  • `git check-ignore -v -n /modules/TC-JSON-API/storage/app/.gitignore` returns me: `/modules/TC-JSON-API/storage/app/.gitignore:2:!.gitignore /modules/TC-JSON-API/storage/app/.gitignore` **in few words: this is "reincluded" by itself...** – Joseph Jan 02 '16 at 18:41

2 Answers2

2

*.gitignore will work, however git will still track .gitignore files it was already tracking


The only different between the two commands being ran is that I add *.gitignore (via the command you see in the middle) to my root level .gitignore file.

Showing *.gitignore works


An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.

from https://git-scm.com/docs/gitignore

Meaning it might be possible that a middle level .gitignore file(s) has !*.gitignore in it, which might be messing up the higher level declaration.

Community
  • 1
  • 1
mawalker
  • 2,072
  • 2
  • 22
  • 34
  • I just tested it on windows git install with gitbash, it worked fine for me. make a BRAND NEW .gitignore file somewhere and see if git status shows it when you have this in your root .gitignore (doesn't for me) – mawalker Jan 01 '16 at 04:22
  • I've added a pic showing how it works for me, and a possible 'gotcha' that might be causing problems for you. – mawalker Jan 01 '16 at 04:47
  • One of the 3rd party module has a .gitignore that contains a !.gitignore rule... ( could be possible that this rule is applied on entire git project instead of its [sub]directory only ? ) – Joseph Jan 02 '16 at 11:35
  • Apparently that is by design, each submodule controls what is tracked inside of it. So 'parent' .gitignore is only for non-module sections. http://stackoverflow.com/a/5127213/2801237 So why not just add an alias that adds '*.gitignore' to all modules through use of foreach. (you will have to delete each/every gitignore previously possibly ) – mawalker Jan 02 '16 at 11:54
  • you're misunderstooding, 3rd party modules are not git submodules but just folders with sources. however in my "modules" folder i've to exclude everything except some specific folders as i said. In 3rd party projects/folders there are some .gitignore files with !.gitignore rule that seems to affect the entire repository. Since the content of other 3rd party folders are not controlled by me, i cannot change those gitignore files. – Joseph Jan 02 '16 at 12:56
  • Either you have the source or you don't, if you 'have' the source you 'can' edit it/get rid of those rules('allowed' is a different thing obviously/unfortunately). If you 'have' the source but aren't actually developing it, then you should be putting them in submodules (that is what they are designed for) and not doing that is just asking for troubles (like what you are experiencing. I'm not sure if .gitignore counter-rules are postfix/prefix/infix/global/sub folder only/etc. If you put them in submodules, then they would be self-contained and wouldn't bother your main repo – mawalker Jan 02 '16 at 13:06
  • I've the source but they are "plugins/modules" handled by an installer ...any change could be removed by an update .. any dev/user can decide wich module should be installed so i cannot use submodules. – Joseph Jan 02 '16 at 13:49
  • It seems a lack of git i think, it allows ignored folders to "arbitrary" un-ignore itself. Ignored folders normally could contains files that are dynamic , temporary or 3rd party ( cache, temp, plugins etc ) ... so git allows to create unwanted behaviours just using a simple !.gitignore inside a .gitignore file – Joseph Jan 02 '16 at 18:49
1

The problem is .gitignore files within the /modules directory:

git check-ignore -v -n /modules/TC-JSON-API/storage/app/.gitignore returns: /modules/TC-JSON-API/storage/app/.gitignore:2:!.gitignore /modules/TC-JSON-API/storage/app/.gitignore

The solution then was just adding /modules/* to my .gitignore:

# exclude everything ...
*
# ...except
!/.gitignore
!*/
!/modules/wp-azth/**

became

# exclude everything ...
*
modules/*
# ...except
!/.gitignore
!*/
!/modules/wp-azth/**

i didn't understand why git needs this kind of "specification" ...without that rule only .gitignore files of ignored folder are processed/listed in commit. However now it works.

Guildencrantz
  • 1,875
  • 1
  • 16
  • 30
Joseph
  • 1,029
  • 13
  • 26
  • 1
    This is because you exclude all files with `*` and then include all directories with `!*/`. Any `.gitignore` file in an included directory will be processed. By explicitly stating that `modules/*` be excluded you're no longer including the `/modules/TC-JSON-API/storage/app/` directory, so git won't include `/modules/TC-JSON-API/storage/app/.gitignore` any more. – Guildencrantz Jan 04 '16 at 21:30