TExcelTAD
Home › Git › Git .gitignore

Git .gitignore

Every project produces files that should never be committed: build output, dependency folders, logs, editor settings, secrets. A file named .gitignore lists patterns for files Git should ignore. Ignored files don't show up in git status and aren't added by git add ..

A first .gitignore

bash · try it yourself
mkdir webapp && cd webapp && git init -q
mkdir -p node_modules/react dist logs
touch node_modules/react/index.js dist/bundle.js logs/app.log logs/error.log
touch server.js .env .DS_Store
echo "--- before:"
git status -s
cat > .gitignore <<'EOF'
# dependencies and build output
node_modules/
dist/
# logs
*.log
# secrets and OS junk
.env
.DS_Store
EOF
echo "--- after:"
git status -s
--- before:
?? .DS_Store
?? .env
?? dist/
?? logs/
?? node_modules/
?? server.js
--- after:
?? .gitignore
?? server.js

The .gitignore file itself should be committed, so everyone on the team ignores the same files.

Pattern rules

Pattern Matches
secret.txt a file or folder with that name anywhere in the project
/secret.txt only at the root of the project (the folder containing .gitignore)
build/ a folder named build anywhere (not a file named build)
*.log any name ending in .log
temp?.txt temp1.txt, tempA.txt, … (? = one character)
img/*.png PNGs directly in img/, not in its subfolders
docs/**/*.pdf PDFs anywhere under docs/ (** = any number of folders)
!keep.log negation: don't ignore this, even if an earlier pattern did
# text a comment

Later lines win over earlier ones, which is what makes negation work:

bash · try it yourself
mkdir app && cd app && git init -q
mkdir -p logs config
touch logs/debug.log logs/README.log config/settings.json config/settings.example.json
printf 'logs/*.log\n!logs/README.log\nconfig/*.json\n!config/*.example.json\n' > .gitignore
git status -s -uall
?? .gitignore
?? config/settings.example.json
?? logs/README.log

Note: You cannot re-include a file if its parent folder is ignored. With logs/ (folder) instead of logs/*.log, Git never even looks inside, so !logs/README.log has no effect.

Why is this file ignored?

git check-ignore -v names the file, line and pattern responsible, which is invaluable in big projects:

bash · try it yourself
mkdir app && cd app && git init -q
printf '*.tmp\nbuild/\n' > .gitignore
mkdir -p src/build && touch notes.tmp src/build/out.o src/main.c
git check-ignore -v notes.tmp src/build/out.o src/main.c
echo "exit status: $?"
.gitignore:1:*.tmp	notes.tmp
.gitignore:2:build/	src/build/out.o
exit status: 0

src/main.c isn't ignored, so it isn't printed.

Ignoring files that are already tracked

.gitignore only affects untracked files. If a file was committed before you added the pattern, Git keeps tracking it. Remove it from the index with git rm --cached (the file stays on disk):

bash · try it yourself
mkdir app && cd app && git init -q
echo "DB_PASSWORD=hunter2" > .env
echo "print('hi')" > app.py
git add . && git commit -q -m "Initial commit (oops, includes .env)"
echo ".env" > .gitignore
echo "edited" >> .env
git status -s
git rm --cached .env
git add .gitignore
git commit -q -m "Stop tracking .env"
git status -s
ls -A
git ls-files
 M .env
?? .gitignore
rm '.env'
.env
.git
.gitignore
app.py
.gitignore
app.py

Warning: The secret is still in the first commit. If it was pushed, rotate the password. Rewriting history to purge it needs a tool such as git filter-repo.

Other places to ignore files

File Scope Committed? Use for
.gitignore (any folder) the project yes build output, dependencies, logs
.git/info/exclude this clone only no your personal scratch files
core.excludesFile (e.g. ~/.gitignore_global) all your repositories no editor and OS files: .idea/, .vscode/, .DS_Store, Thumbs.db
bash
git config --global core.excludesFile ~/.gitignore_global

GitHub's github/gitignore repository has ready-made templates for most languages and frameworks, and the New repository page can add one for you.

Test yourself

Which pattern ignores every .log file in the project?

  1. /log
  2. *.log
  3. log/
  4. !*.log

You add config.json to .gitignore, but Git still shows changes to it. Why?

  1. Patterns need a leading slash
  2. The file was already tracked; run git rm --cached config.json
  3. .gitignore must be committed first
  4. Git ignores only folders

Which command tells you which .gitignore line ignores a file?

  1. git status --ignored
  2. git check-ignore -v <file>
  3. git ls-files
  4. git log -- .gitignore