Git and GitHub · Part 1 · Git basics
Ignoring files
What shouldn't be in Git
Every project has files that should never be committed:
| Item | Why it stays out |
|---|---|
node_modules/ | Huge, and npm install recreates it |
.env | Secrets: API keys and passwords |
.next/, dist/, build/ | Generated on every build |
.DS_Store | Junk macOS adds to folders |
*.log | Debug logs |
git add . picks all of it up, which is how API keys end up on GitHub. A .gitignore file prevents that. Like a “do not pack” list for movers, it tells Git which files to act as if they don't exist.
Trying it out
1.Make a messy project
cd ~/Desktop
mkdir ignore-practice
cd ignore-practice
git init
echo "console.log('app')" > app.js
echo "API_KEY=super-secret-123" > .env
echo "error at line 5" > debug.log
mkdir node_modules dist
echo "fake package" > node_modules/react.js
echo "built file" > dist/bundle.js
git statusUntracked files:
.env
app.js
debug.log
dist/
node_modules/Git is ready to track everything, the secret included.
2.Add a .gitignore
Create .gitignore in the project's root (code .gitignore), with one pattern per line. Lines starting with # are comments.
# Dependencies
node_modules/
# Secrets
.env
# Build output
dist/
# Logs
*.log3.Check again and commit
git statusUntracked files:
.gitignore
app.jsThe ignored files are still on disk; Git just doesn't see them. Commit the .gitignore itself, so the whole team ignores the same things:
git add .
git commit -m "Add app and gitignore"Pattern rules
| Pattern | Ignores | Matches |
|---|---|---|
secret.txt | That file, in any folder | secret.txt, src/secret.txt |
logs/ | That folder, anywhere | logs/, src/logs/ |
/todo.txt | Only in the root | todo.txt, not src/todo.txt |
*.log | Any name ending in .log | debug.log, error.log |
**/temp | temp at any depth | temp, a/b/c/temp |
build/*.js | .js files directly in the root build/ | build/app.js |
!keep.log | An exception: don't ignore it | keep.log, even with *.log |
One limit of !: it can't bring back a file inside an ignored folder. With logs/ ignored, !logs/keep.log does nothing.
To see which rule ignores a file:
git check-ignore -v debug.log.gitignore:11:*.log debug.logLine 11 of .gitignore, the *.log rule.
The biggest trap: files already tracked
.gitignore only affects files Git isn't tracking yet. A file that was committed before being added to it stays tracked:
echo "my notes" > notes.txt
git add notes.txt
git commit -m "Add notes"
echo "notes.txt" >> .gitignore
echo "more notes" >> notes.txt
git statusmodified: .gitignore
modified: notes.txtThe fix is git rm --cached, which stops tracking the file but keeps it on disk. The “deleted” in the status means deleted from Git only:
git rm --cached notes.txt
git commit -am "Stop tracking notes.txt"For a folder, such as a committed node_modules, add -r: git rm -r --cached node_modules.
When a secret was committed
Removing a committed .env doesn't remove it from the history: git show <old-hash>:.env still shows the key.
- Replace the key right away in the service it belongs to, and treat the old one as stolen. This is the step that matters most.
- Not pushed yet: take the commit out with
git reset(Lesson 7). - Already pushed: bots scan GitHub for keys within minutes, so assume it's leaked. Tools like
git filter-repocan clean the history, but replacing the key is the real fix.
Sharing variable names with .env.example
Teams commit a .env.example with the variable names and no values, and each developer copies it to .env and fills in their own:
DATABASE_URL=
STRIPE_SECRET_KEY=
NEXT_PUBLIC_API_URL=DATABASE_URL=postgres://real-password@...
STRIPE_SECRET_KEY=sk_live_abc123...Ignoring .DS_Store everywhere
Personal junk like .DS_Store belongs in a global ignore file, which applies to every repository on the computer:
echo ".DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_globalThe project's .gitignore is for what the whole team ignores; the global file is for one person's OS and editor files.
Ready-made templates
create-next-app, Vite and the NestJS CLI create a .gitignore automatically, GitHub offers one when creating a repository, and github.com/github/gitignore has templates for almost every framework. The main parts of a Next.js one:
# dependencies
/node_modules
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
# debug
npm-debug.log*
# env files
.env*
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.tsKeeping an empty folder
Git tracks files, not folders. To keep an empty folder like uploads/, add a placeholder file. .gitkeep is the usual name, though it's only a convention:
touch uploads/.gitkeepQuick reference
| To… | Do this |
|---|---|
| Ignore a file or folder | Add it to .gitignore |
| Ignore by extension | *.log |
| Ignore all but one | !keep.log |
| See why a file is ignored | git check-ignore -v file |
| Stop tracking a committed file | git rm --cached file |
| Stop tracking a folder | git rm -r --cached folder |
| Ignore OS junk everywhere | ~/.gitignore_global |
| Share env variable names | Commit .env.example |