Git
Tutorial: create a .gitignore to stop uploading junk files
Set up a .gitignore file to keep node_modules and sensitive files out of your repository.
If you've ever accidentally pushed an entire node_modules folder (with its 40,000 tiny files) to GitHub, this one's for you. If not, even better, this tutorial saves you that public embarrassment.
Step 1: create the file
At the root of your project, create a file named exactly .gitignore (with the leading dot, no extension).
touch .gitignore
Step 2: tell it what to ignore
# Dependencies
node_modules/
# Environment files with sensitive data
.env
# OS-generated files
.DS_Store
# Build folders
dist/
.next/
Each line is a pattern: node_modules/ ignores that entire folder, .env ignores that specific file. Git will pretend those files don't exist for the purposes of git add and git status.
Step 3: what if you already uploaded those files?
.gitignore only works on files Git isn't already tracking. If you accidentally uploaded them before, you need to untrack them (without deleting them from your computer):
git rm -r --cached node_modules
git commit -m "Stop tracking node_modules"
--cached is the key word here: it removes the file from Git's repository, but leaves it untouched on your hard drive. Nobody loses anything, except your repo's dead weight.
Step 4: verify it's working
git status
If node_modules no longer shows up in the list of new or modified files, your .gitignore is doing its job correctly.
What did you just learn?
- What the
.gitignorefile is for and how to create it. - What kinds of files you should always ignore (dependencies, environment files, builds).
- How to untrack files you already pushed by mistake with
git rm --cached.