Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout with different node_modules folder

I have two branches, one called "gh-pages" and the other "master" branch. Both are having separate folders like "node_modules", but they are completely different. And they are untracked!

So when checking out e.g. the "gh-pages" branch as follows

git checkout gh-pages

I need a way to specify that Git should not just remove (but save) all tracked, but also all untracked files and folders (like the "node_modules" folder) from the "master" branch. And then load all tracked files and folders from the "gh-pages" branch and as well as all untracked files and folders.

Is there a simple way or do I really need to remove untracked files and folders for each checkout so that the two do not come into the cross?

like image 465
user3631654 Avatar asked May 09 '16 11:05

user3631654


People also ask

Can we checkout specific folder in git?

You probably knew that a branch can be "checked out" with the command git checkout <branch-name> , but interestingly enough, a single file or a whole folder can also be checked out from another branch.

Does git checkout change working directory?

Git Checkout File Checking out a file is similar to using git reset with a file path, except it updates the working directory instead of the stage. Unlike the commit-level version of this command, this does not move the HEAD reference, which means that you won't switch branches.

Should node_modules be checked into git?

For reference, npm FAQ answers your question clearly: Check node_modules into git for things you deploy, such as websites and apps. Do not check node_modules into git for libraries and modules intended to be reused. Use npm to manage dependencies in your dev environment, but not in your deployment scripts.

How do I force checkout to another branch?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.


1 Answers

I suffered the problem too. And my workaround is using git worktree. Create linked working tree for each branch, put the node_modules in it instead of main working tree.

See this similar question: Switch node_modules folder when I change git branch

This article explains why it is better than git clone: https://spin.atomicobject.com/2016/06/26/parallelize-development-git-worktrees/

like image 192
周汉成 Avatar answered Oct 03 '22 08:10

周汉成