A little-known but super-useful tool in the developer’s arsenal is the .gitignore file. It’s a list of files and folders that are purposely ignored during commits and pulls, which can greatly improve code quality. In this article, we’ll discuss what goes into a .gitignore file and how to use .gitignore file to remove node_modules folder from your project.
What is .gitignore file?
This .gitignore file is text files that are found within your repository on Git. It is used for a variety of purposes and you’ll almost always have to set it up when you set up the repository for the first time. Additionally, additional entries can include in .gitignore for ease of use.
Why should use .gitignore file to remove node_modules Folder?
For Node JS projects, you will find the folder named Node_modules which includes all the packages that your code requires to run. It is possible to delete the directory and then rebuild it from scratch by using the npm install and making use of your package.json configuration to locate the necessary packages.
So what’s the benefit of having the node_modules directory in Git? There really isn’t one that, since it’s What’s the advantage to using an Node_modules folder within Git? There’s no reason to doubt that it isn’t, because it’s more complicated could cause issues and could increase dramatically the size of the Git repository in a lot of cases.
If you don’t need include all directories, your colleagues will still be able to create their own local copies from your package.json file. This means that the only package.json should be monitored in Git. The majority of the items that are added into .gitignore follow this same pattern.
Steps to remove node_modules folder with .gitignore file
You’ll need in order to make an image file titled .gitignore with no extension, in the base of your project, which is where you will find the .git directory is. You can do this using your file browser or the command line by using CLI Text editor.
Then enter the node_modules/ folder name in the .gitignore file to remove it, like this:
In case the folder contains many subfiles in it, the operation still works properly.
If your already got an existing
Node Modules/ folder that is tracked by Git, it is possible to make Git ignore the directory by taking it from the Git cache by using these commands:
git rm -r --cached .
The command above will delete all cached files tracked by Git. If you want to add them back you can use the git add command. Then you need to reconfirm to commit the changes and then put them back into the remote storage directory.
Here are the full git commands you can refer to:
git rm -r --cached . git add . git commit -m "Remove node_modules folder" git push
Above we have shown you the steps to Use .gitignore file To Remove node_modules Folder. If you encounter any difficulties during the operation, do not hesitate to contact us or leave a comment below. Thanks for reading!