1

I have two branches which contain their own version of node_modules, but in different folders.

Branch 1:

--client/
--server/
--node_modules/
...

Branch 2:

--server/
-----/node_modules/
...

So, when I switched from Branch 2 -> Branch 1, it contained the following folders:

--client/
--server/
-----/node_modules/
--node_modules/
...

This made an error because Node will use the nested version instead of the outer one. So, my question is how to disappear files specified in .gitignore, for example node_modules in this case, when checking out another branch?

lvarayut
  • 13,963
  • 17
  • 63
  • 87

1 Answers1

1

You can try (dry-run test)

git clean -d -x -n

To see, just after a git checkout, if that would remove the right files.
See "How do I clear my local working directory in git?"

The other approach would be a git reset --hard right after the git checkout.


If you cannot easily remove the (.gitignore'd) node_modules folder, then a third approach would be to work in tow different folders, one for each branch.
See "Multiple working directories with Git?": since Git 2.5+, you can clone once, and checkout different branches in different folders.
Switching branches becomes as easy as a cd ../otherbranch.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Does this mean the `node_modules` will be deleted? If so, could we only make it disappear for a while and get it back whenever we switch back? Because I sometimes need to switch to another branch to do just simple thing, and then, switch back. I don't want to do `npm install` or `git pull` every time I switch. – lvarayut Nov 20 '15 at 09:04
  • @LVarayut then it is best to keep `node_modules` generated content (by npm install) *outside* the repo (not versioned) – VonC Nov 20 '15 at 09:25
  • Not sure if I understood correctly but `node_modules` is already ignored by using `.gitignore`. Did you mean to keep it outside the current working directory? If so, I'm not quite sure if it's a good idea. – lvarayut Nov 20 '15 at 10:08
  • @LVarayut Then I have edited my answer to propose an alternative. – VonC Nov 20 '15 at 10:24
  • Thanks very much for your help. – lvarayut Nov 21 '15 at 03:04