Using .gitignore Files
Table of contents
- What is .gitignore?
- Basic syntax
- Common patterns
- Templates by language
- Global .gitignore
- Common problems
- Best practices
1 - What is .gitignore?
The .gitignore file tells Git which files or folders not to track.
Why ignore files?
| File type | Example | Reason |
|---|---|---|
| Dependencies | node_modules/ | Too large, recreatable |
| Build | dist/, build/ | Automatically generated |
| Secrets | .env, secrets.json | Security! |
| IDE | .vscode/, .idea/ | Personal preferences |
| OS | .DS_Store, Thumbs.db | System files |
| Logs | *.log | Temporary |
| Cache | __pycache__/, .cache/ | Recreatable |
Create a .gitignore
# À la racine du projet
touch .gitignore
🔝 Back to table of contents
2 - Basic syntax
Simple rules
# Ceci est un commentaire
# Ignorer un fichier spécifique
secret.txt
# Ignorer un dossier
node_modules/
# Ignorer par extension
*.log
*.tmp
# Ignorer dans tous les sous-dossiers
**/temp/
# Négation (ne PAS ignorer)
!important.log
Pattern syntax
| Pattern | Meaning | Example |
|---|---|---|
file.txt | Exact file | file.txt |
*.txt | All .txt files | readme.txt, notes.txt |
folder/ | Entire folder | folder/ and its contents |
folder/* | Folder contents | Files in folder/ |
**/logs | logs anywhere | a/logs, a/b/logs |
logs/** | Everything in logs | logs/a, logs/a/b |
!file.txt | Exception | Do not ignore file.txt |
[abc].txt | a.txt, b.txt, c.txt | Character class |
file?.txt | file1.txt, fileA.txt | Any single character |
🔝 Back to table of contents
3 - Common patterns
Secret configuration files
# Variables d'environnement
.env
.env.local
.env.*.local
# Fichiers de secrets
secrets.json
credentials.json
*.pem
*.key
Dependencies
# Node.js
node_modules/
package-lock.json # Optionnel
# Python
venv/
__pycache__/
*.pyc
# PHP
vendor/
# Java
target/
*.jar
Build and output
# Build
dist/
build/
out/
*.min.js
*.min.css
# Coverage
coverage/
.nyc_output/
IDEs and editors
# VS Code
.vscode/
!.vscode/settings.json # Garder les settings partagés
# JetBrains (IntelliJ, WebStorm, PyCharm)
.idea/
*.iml
# Vim
*.swp
*.swo
# Emacs
*~
Operating system
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
*~
.nfs*
🔝 Back to table of contents
4 - Templates by language
Node.js / JavaScript
# Dependencies
node_modules/
# Build
dist/
build/
# Environment
.env
.env.local
.env.*.local
# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# IDE
.vscode/
.idea/
# OS
.DS_Store
Thumbs.db
# Testing
coverage/
# Cache
.cache/
.parcel-cache/
Python
# Byte-compiled
__pycache__/
*.py[cod]
*$py.class
# Virtual environment
venv/
ENV/
.venv/
# Distribution
dist/
build/
*.egg-info/
# IDE
.idea/
.vscode/
*.swp
# Jupyter
.ipynb_checkpoints/
# Environment
.env
# Testing
.pytest_cache/
.coverage
htmlcov/
Java / Spring Boot
# Build
target/
build/
# IDE
.idea/
*.iml
.project
.classpath
.settings/
# Logs
*.log
# Maven
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
# Gradle
.gradle/
gradle-app.setting
!gradle-wrapper.jar
Resource: gitignore.io
Automatically generate a .gitignore:
👉 https://www.toptal.com/developers/gitignore
🔝 Back to table of contents
5 - Global .gitignore
Global configuration
To ignore files across all your projects:
# Créer le fichier global
touch ~/.gitignore_global
# Configurer Git pour l'utiliser
git config --global core.excludesFile ~/.gitignore_global
Recommended content for ~/.gitignore_global
# macOS
.DS_Store
.AppleDouble
.LSOverride
._*
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# IDE
.idea/
*.swp
*.swo
*~
# Logs généraux
*.log
When to use global vs local?
| Type | Global | Local (project .gitignore) |
|---|---|---|
| OS files | ✅ | ❌ |
| Personal IDE preferences | ✅ | ❌ |
| Project dependencies | ❌ | ✅ |
| Project config | ❌ | ✅ |
| Build files | ❌ | ✅ |
🔝 Back to table of contents
6 - Common problems
Problem 1: The file is already tracked
Symptom: You add a file to .gitignore but it is still tracked.
Cause: The file was added before the .gitignore.
Solution:
# Retirer du suivi (garder le fichier localement)
git rm --cached fichier.txt
# Pour un dossier
git rm -r --cached dossier/
# Commiter
git commit -m "chore: stop tracking fichier.txt"
Problem 2: Ignore everything except certain files
# Ignorer tout dans logs/
logs/*
# Sauf .gitkeep
!logs/.gitkeep
Problem 3: .gitignore doesn't work
Checks:
- Is the file correctly named
.gitignore(with the dot)? - Are there any trailing spaces at the end of lines?
- Does the file have the right encoding (UTF-8)?
# Vérifier ce qui est ignoré
git check-ignore -v fichier.txt
# Voir tous les fichiers ignorés
git status --ignored
Problem 4: Secret committed by mistake
# 1. Ajouter au .gitignore
echo "secret.env" >> .gitignore
# 2. Retirer du suivi
git rm --cached secret.env
# 3. Commiter
git commit -m "security: remove secret from tracking"
# 4. IMPORTANT : Changer les secrets compromis !
# L'historique Git contient toujours le fichier
⚠️ To completely remove a secret from the history, use
git filter-branchor BFG Repo-Cleaner.
🔝 Back to table of contents
7 - Best practices
✅ Do
- Create the .gitignore at the start of the project
- Use templates (gitignore.io)
- Never commit secrets
- Document unusual ignored files
❌ Avoid
- Ignoring essential files (package.json, requirements.txt)
- Ignoring the .gitignore itself
- Ignoring shared config files (.editorconfig)
Recommended project structure
mon-projet/
├── .gitignore # Ignorer fichiers du projet
├── .env.example # Template pour .env (commité)
├── .env # Variables réelles (ignoré)
├── package.json # Commité
├── node_modules/ # Ignoré
├── dist/ # Ignoré
└── src/
└── ...
Complete .gitignore example
# ===============================
# Dependencies
# ===============================
node_modules/
vendor/
# ===============================
# Build & Output
# ===============================
dist/
build/
*.min.js
*.min.css
# ===============================
# Environment & Secrets
# ===============================
.env
.env.local
.env.*.local
*.pem
secrets/
# ===============================
# IDE & Editors
# ===============================
.idea/
.vscode/
*.swp
*.swo
# ===============================
# OS Files
# ===============================
.DS_Store
Thumbs.db
# ===============================
# Logs & Cache
# ===============================
logs/
*.log
.cache/
# ===============================
# Testing
# ===============================
coverage/
.nyc_output/