Git and GitHub · Part 1 · Git basics

Ignoring files

What shouldn't be in Git

Every project has files that should never be committed:

ItemWhy it stays out
node_modules/Huge, and npm install recreates it
.envSecrets: API keys and passwords
.next/, dist/, build/Generated on every build
.DS_StoreJunk macOS adds to folders
*.logDebug 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

Terminal
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 status
Output
Untracked 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.

.gitignore
# Dependencies
node_modules/

# Secrets
.env

# Build output
dist/

# Logs
*.log

3.Check again and commit

Terminal
git status
Output
Untracked files:
        .gitignore
        app.js

The ignored files are still on disk; Git just doesn't see them. Commit the .gitignore itself, so the whole team ignores the same things:

Terminal
git add .
git commit -m "Add app and gitignore"

Pattern rules

PatternIgnoresMatches
secret.txtThat file, in any foldersecret.txt, src/secret.txt
logs/That folder, anywherelogs/, src/logs/
/todo.txtOnly in the roottodo.txt, not src/todo.txt
*.logAny name ending in .logdebug.log, error.log
**/temptemp at any depthtemp, a/b/c/temp
build/*.js.js files directly in the root build/build/app.js
!keep.logAn exception: don't ignore itkeep.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:

Terminal
git check-ignore -v debug.log
Output
.gitignore:11:*.log	debug.log

Line 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:

Terminal
echo "my notes" > notes.txt
git add notes.txt
git commit -m "Add notes"

echo "notes.txt" >> .gitignore
echo "more notes" >> notes.txt
git status
Output
modified:   .gitignore
        modified:   notes.txt

The 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:

Terminal
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.

  1. 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.
  2. Not pushed yet: take the commit out with git reset (Lesson 7).
  3. Already pushed: bots scan GitHub for keys within minutes, so assume it's leaked. Tools like git filter-repo can 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:

.env.example (committed)
DATABASE_URL=
STRIPE_SECRET_KEY=
NEXT_PUBLIC_API_URL=
.env (ignored)
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:

Terminal
echo ".DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

The 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:

.gitignore
# 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.ts

Keeping 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:

Terminal
touch uploads/.gitkeep

Quick reference

To…Do this
Ignore a file or folderAdd it to .gitignore
Ignore by extension*.log
Ignore all but one!keep.log
See why a file is ignoredgit check-ignore -v file
Stop tracking a committed filegit rm --cached file
Stop tracking a foldergit rm -r --cached folder
Ignore OS junk everywhere~/.gitignore_global
Share env variable namesCommit .env.example