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?
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With