If you are using command line you can use below commands in the terminal of the project directory:
touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ; git status
touch will create .gitignore file if it doesn’t exist
echo and >> will append node_modules/ at the end of.gitignore file
After node_modules/ folder is added to the .gitignore file, this folder and all subfoders will be ignored
git rm -r –cached removed the node_modules folder if it was already added
git status will display the new changes
You can also add config files that include sensitive information such as database details or environment specific information.
For example
# dependencies
/node_modules
# configs
.env.test
.env.production