5

I just have done a git push in our online repo

When I checked the online repo and checked the public folder it shows this: enter image description here

while in my local repo I have this in my public folder:

enter image description here

My local has Storage folder from my storage link. I am guessing that this has something to do with the .gitignore inside my local public folder. It contains:

*
!.gitignore

Same goes with my storage folders. In our Online repo inside storage/app/public: enter image description here

while in my local storage folder:

enter image description here

user_images is present in my local repo. Here is the inner .gitignore:

*
!.gitignore

and the outer .gitigore:

*
!public/
!.gitignore

I am not quite familiar with setting rules in .gitignore. I wish to make sure that whatever my local Storage and public folder has, the Online repo has as well. How should I go about in doing this?

Kindly edit my tags if it is misleading or incorrect. Thank you.

This is the content of the root .gitignore:

/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log

I have removed /public/storage in .gitignored and tried to do a git push

Changes not staged for commit:
        modified:   .gitignore

and still, nothing is the same from my local to Online repo? Anything I might have missed?

Tried running git check-ignore -v path/to/file

In my root .gitignore:

/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log

If I ran git check-ignore -v public/hot, it replies with .gitignore:2:/public/hot public/hot but if I ran git check-ignore -v public/storage, it doesn't respond with anything which it should because it is included in root .gitignore I come to believe that this situation made it impossible for the public/storage/user_images and storage/app/public/user_images. Any help is needed.

I may have been viewing a wrong file or path all this time. But I can confirm that @VonC method helped me do a proper exclusion in gitignore. Thanks.

EDIT:

Final gitignores so far

root .gitignore:

/node_modules
# /public/hot
# /public/storage
/storage/*.key
/vendor
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log

storage\app.gitignore

# *
# !public/
!.gitignore

storage\app\public.gitignore

# *
!.gitignore

public\storage.gitignore

# *
!.gitignore

Just an additional info:

git check-ignore -v vendor gives .gitignore:5:/vendor vendor

while git check-ignore -v /vendor gives fatal: C:/Program Files/Git/vendor: 'C:/Program Files/Git/vendor' is outside repository

I'm not sure where git check-ignore -v path points to.

KiritoLyn
  • 626
  • 1
  • 10
  • 26
  • The storage folder inside your local screenshot has user uploaded images. I do not think that will be a a good data to push into repo, why is it needed? – Mihir Bhende Feb 06 '19 at 03:17
  • because in my project, if the user doesn't upload an image, that `noimage.jpg` will be used as default. What are your suggestions? – KiritoLyn Feb 06 '19 at 03:19
  • Is there a way we can include `noimage.jpg` as an exemption? or if not at least everything else in my local reflects exactly to the online repo? – KiritoLyn Feb 06 '19 at 03:26
  • What is the content of ,gitignore inside the laravel root? – Mihir Bhende Feb 06 '19 at 03:28
  • I have included the contents of root `.gitignore` in my post. I hope it helps. – KiritoLyn Feb 06 '19 at 03:32
  • 1
    you have `/public/storage` in .gitignore and hence the storage folder is ignored in the push :) – Mihir Bhende Feb 06 '19 at 03:33
  • Oh I see, so it goes without saying that I should also remove `/storage/*.key` to reflect all my local folders in storage to online storage folder? Or the `/public/storage` is enough to solve both `public` and `storage` folder synching? – KiritoLyn Feb 06 '19 at 03:39
  • I would say move the image outside storage folder and put it inside public/img folder – Mihir Bhende Feb 06 '19 at 03:40
  • wait why? That would cause all my `img src` path to mess up? Is there a way to exclude storage folder altogether? – KiritoLyn Feb 06 '19 at 03:43
  • 1
    If you really want then you can just remove the `public/storage` entry from gitignore and push the changes. It will automatically show you to add the storage folder it will show in red as untracked. – Mihir Bhende Feb 06 '19 at 03:46
  • I will try that as recommended, I'll also see if it includes the `noimage.jpg` – KiritoLyn Feb 06 '19 at 03:47
  • Updated result in the post – KiritoLyn Feb 06 '19 at 03:58
  • Now if you do git status, can you see public/storage in red ? – Mihir Bhende Feb 06 '19 at 03:59
  • Yes it says: `Changes not staged for commit: modified: .gitignore` – KiritoLyn Feb 06 '19 at 04:02
  • do `git add .gitignore public/storage` and `git push` – Mihir Bhende Feb 06 '19 at 04:03
  • after doing `git push` it said `Everything up-to-date` and when i did a `git status` it says `Changes to be committed: modified: .gitignore` in green color. – KiritoLyn Feb 06 '19 at 04:07
  • ohh did you do git commit after add and before push? – Mihir Bhende Feb 06 '19 at 04:08
  • Do not keep resources in storage. Add a new folder under assets which you copy on build to the public folder. – Michael Mano Feb 06 '19 at 04:27
  • @MihirBhende I did a `git add .gitignore public/storage`, `git commit -m "test"`, `git push` and checked online repo but it still has no `public\storage\user_images` and `storage\app\public\user_images` where am I doing it wrong? – KiritoLyn Feb 06 '19 at 05:14

2 Answers2

6

First, for any file ignore within a folder, type:

git check-ignore -v -- path/to/file

That will display exactly which .gitignore is responsible for ignoring said file.

Second, the rule of gitignore is simple:

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

To exclude files (or all files) from a subfolder of an ignored folder f, you would do:

f/**
!f/**/
!f/a/sub/folder/someFile.txt

Meaning: you need to whitelist folders first, before being able to exclude from gitignore files.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I did `git check-ignore -v storage/app/user_images` and it responded `storage/app/.gitignore:1:* storage/app/user_image` so what i did is commented the `# *` so upon doing a `check-ignore` again nothing showed. proceeded in doing a: `git add *`, `git commit -m "test"`, `git push` but still the online repo has no `storage/app/user_images` directory. Am I doing anything wrong here? – KiritoLyn Feb 06 '19 at 06:08
  • @JustinF As long as nothing shows, keep doing `git check-ignore -v`: you will see what rule block. Then re-read the rule I mention: any folder content ifnored is generally ignored because a *parent* folder was already ignored itself. – VonC Feb 06 '19 at 06:11
  • something weird is happening. I edited root `.gitignore` in my project, I have these two lines: `/public/hot`, `/public/storage` when I check-ignore public/hot is replies `.gitignore:2:/public/hot public/hot` but if I check-ignore public/storage it does not reply anything even though it is clearly included in the `.gitignore` file. Is there anything unusual happening here? – KiritoLyn Feb 06 '19 at 06:38
  • @JustinF Check-ignore a *file*: Git does not version folders. As a best practice, when ignoring folders (content), always put a trailing '/' in your .gitignore rules (for folders): `/public/storage/` or `/public/hot/` – VonC Feb 06 '19 at 07:46
  • @JustinF Also, what were the final gitignore rules you have set up? – VonC Feb 06 '19 at 07:47
  • 1
    @JustinF I confirm the check-ignore only takes *relative* paths from the root of the git repo, not absolute paths. – VonC Feb 06 '19 at 09:00
  • Can you explain further what it was meant by _relative_ paths from the root? I didn't quite get it. – KiritoLyn Feb 06 '19 at 13:32
  • @JustinF in your case, if you `cd` to the root folder of your repo, the `git check-ignore` command would use `public/storage` (relative to that current folder), *not* `/public/storage`. – VonC Feb 06 '19 at 13:41
  • basically `git check-ignore` means `project-name/` and not just `project-name` (without slash)? is this correct? – KiritoLyn Feb 06 '19 at 13:51
  • 1
    @JustinF means `afile`, or `afolder/`, in that (for the folder case) `git check-ignore` will test if *files* within that folder are ignored by a `.gitignore` rule. But in both cases, the path to a file (or the path to a folder) passed as an argument would be relative to your current folder, inside the repo. – VonC Feb 06 '19 at 13:59
2

For those who might still be searching for an answer, edit the .gitignore files in storage/app and storage/app/public folders and remove the first 2 lines from each.

salmanhijazi
  • 817
  • 2
  • 13
  • 25