6

I am having a problem with gitignore in Laravel 5.3.

I have the following gitignore file. I used ! to exclude myfile.json.

node_modules/
public/storage
storage/*.key
/.idea
Homestead.json
Homestead.yaml
.env
SQL/
*.sql
mynotes.md
!storage/app/mydir/myfile.json

When I run git status --ignored I get this outputs.

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

    ../.env
    ../SQL/
    ../app/.DS_Store
    ../bootstrap/cache/config.php
    ../bootstrap/cache/services.php
    ../mynotes.md
    ../node_modules/
    uploads/.DS_Store
    ../storage/app/.DS_Store
    ../storage/app/mydir/
...
...

So myfile.json is ignored. How can I write a gitignore so that I can add/commit a file?

shin
  • 31,901
  • 69
  • 184
  • 271

1 Answers1

2

The rule to remember with gitignore:

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

So check which .gitignore rule actually does ignore ../storage/app/mydir/, because if it is a .gitignore located (for instance) directly in /storage/app/, it will have precedence.

git check-ignore -v -- ../storage/app/mydir/myfile.json

../ means your current .gitignore is not in the right place: for a !storage/app/mydir/xxx rule to apply, it should be in a .gitignore file one folder up.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Actually it was one level down I found another gitignore by `git check-ignore -v -- storage/app/mydir/myfile.json storage/app/.gitignore:1:* storage/app/mydir/myfile.json`. So I found another gitignore in storage/app/ dir. Can I add here !storage/app/mydir/myfile.json? – shin Jan 03 '17 at 06:33
  • 1
    Yes, that would be the right place to add that rule. – VonC Jan 03 '17 at 06:36
  • 1
    Thanks. BTW when I modify the gitignore and `git status`, the myfile.json does not appear. Do I need to first add/commit this gitignore to take effect? – shin Jan 03 '17 at 06:38
  • 1
    No, any modification to a gitignore is immediately taken into account by git status. Use check-ignore again. – VonC Jan 03 '17 at 06:42