How to Remove node_modules Already Committed

1 minute read

Motivation

When working with JavaScript (or TypeScript in my case specifically), we get a very huge folder named node_modules. The rule of thumb is that we don’t upload such a huge directory to a remote repo simply because it’s large. I accidentally pushed a commit where I added node_modules and then tried to remove it from GitHub, but things got pretty wacky. And this is what I found to delete node_modules that’s already been committed.

If you don’t know what .gitignore is

.gitignore is a file you can write things you don’t want to upload to your remote repo (short for repository) on GitHub. Hence, the name “git” + “ignore” = “gitignore”.

To ignore node_modules, we make a .gitignore file and write:

node_modules/

And once you’ve written the above, node_modules directory will not be uploaded from now on. But what if you already have node_modules living in the remote repository?

How to remove node_modules from the remote repo

Just run the following command:

$ git rm -r --cached .

Then, you update the new change:

$ git add .
$ git commit -m "fix: Remove node_modules dir"
$ git push

Thanks for reading, and I’ll come back with another post soon.

Tags:

Categories:

Updated:

Leave a comment