6

I have a git repository setup in git lab. Right now each time I change branch i should do:

npm install && composer install && cp .env.example .env && artisan generate key

Cause I lose .env , node_modules and composer modules. and it takes long time reinstalling them. cause I cant run it and test the branch if I dont have node_modules and other stuff installed

I wonder if Im doing something wrong or if there is a way to make it happen.

I have done lots of search but no luck.

Thanks in advance

Mishel Parkour
  • 599
  • 5
  • 16

1 Answers1

4

Are you sure the files / directories you are talking about are ignored by git (they are in your .gitignore file)? If that's not the case, here is the answer to your question:

Since they are bound to the environment you are working on, they should not be touched by git by any means. That's why you should not lose them if you checkout on another branch.

Only the composer.lock, the package-lock.json and the .env.example should be versioned. Then, when you clone the repo from GitLab, you do a npm install, a composer install, you copy the .env.example etc... in order to setup your dependencies, but the dependencies directories (eg. node_modules) should not come from your repository.

Then after a while, let's imagine you want to update your Composer dependencies. You'll do a composer update. Your composer.lock file will be updated and will be committed to your repository.

Then, if somebody on another computer pulls your changes, he will only pull the newly updated composer.lock file. He will then make a composer install, which will install (or update if he already had installed them before) the dependencies from the composer.lock into his vendor folder.

I hope it helps you, feel free to ask more details in the comments :)

Anthony Aslangul
  • 3,589
  • 2
  • 20
  • 30
  • thanks for your answer but my question is. what if I want to change branch? then as you know all files in directory are gonna get changed. lets say I didnt change modules installed so `package-lock` and `composer.lock` are not changed. then when I change branch I lose node_modules folder and I have to do another npm install to install dependencies from `package-lock.json` . so my question is how to avoid `node_modules` from being deleted so then I don't have to run npm install each time I switch between branches? – Mishel Parkour May 01 '19 at 12:23
  • 3
    That's the problem, you should not lose all these files and folders. Changing branch only change the files/folders that are under verson control. If you ignore a file, you can switch branch, delete branch, init a new repo or do anything else, this file - which is ignored - will not be deleted. When you switch branch, can you describe exactly what you are doing? It might be easier to understand what's wrong :) – Anthony Aslangul May 01 '19 at 13:05
  • yes @toyi. thats what I was wondering. however some strange things were happening around. my in my case I wanted to switch from branch A to branch B and it seems that I didnt have node_module folder in branch master or something like that. now my problem is fixed. I will spend some time and find out the details. and then I will either post an answer or let you know about it so you can update your answer and then ill accept it. thanks for your help so far – Mishel Parkour May 02 '19 at 14:53
  • no problem @MishelParkour, if you have any question just post a comment – Anthony Aslangul May 02 '19 at 15:45