Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch node_modules folder when I change git branch

I'm working with two branches, master and feature-1

In the master branch I'm using a lib [email protected]

In the branch feature-1 I've installed a new version from lib xyz version 2.0

feature-1 was not merged yet on master branch, because it's not done. Then when I switch to master again I've to re-install the [email protected], and if I switch again to feature-1 I've to re-install the [email protected]

What's the best workflow approach in theses cases?

like image 435
carlosrberto Avatar asked Aug 11 '16 20:08

carlosrberto


People also ask

Can I move node_modules folder?

You can if you'd like, but it is not considered best practice. In any case, you shouldn't make any modifications to the files inside node_modules . Preferably you need to only copy over your package. json file and optionally, your package-lock.

Should you commit the node_modules folder to git?

Not committing node_modules implies you need to list all your modules in the package. json (and package-lock. json ) as a mandatory step. This is great because you might not have the diligence to do so, and some of the npm operations might break if you don't.

What happens to untracked files when switching branches?

Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!


1 Answers

You do things well with git and each time you switch, you have to do your npm i.

But perhaps if you switch very often from one branch to the other, you should think about using git worktree introduced in some recent git versions and which permit to have another branch checked out in another folder (here feature-1) but sharing the same repository (perfect for this type of work flow!) .

That way you will be able to do the npm i once for each branch and work in parallel on each branch.

It's quite simple to use but I will let you have a look to the documentation.

Once you're finished, just delete the folder with the added workspace (but not the main one with the .git folder!)

like image 157
Philippe Avatar answered Sep 28 '22 01:09

Philippe