Skip to main content

Using .gitignore Files


Table of contents


  1. What is .gitignore?
  2. Basic syntax
  3. Common patterns
  4. Templates by language
  5. Global .gitignore
  6. Common problems
  7. Best practices


1 - What is .gitignore?



The .gitignore file tells Git which files or folders not to track.

Why ignore files?

File typeExampleReason
Dependenciesnode_modules/Too large, recreatable
Builddist/, build/Automatically generated
Secrets.env, secrets.jsonSecurity!
IDE.vscode/, .idea/Personal preferences
OS.DS_Store, Thumbs.dbSystem files
Logs*.logTemporary
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

PatternMeaningExample
file.txtExact filefile.txt
*.txtAll .txt filesreadme.txt, notes.txt
folder/Entire folderfolder/ and its contents
folder/*Folder contentsFiles in folder/
**/logslogs anywherea/logs, a/b/logs
logs/**Everything in logslogs/a, logs/a/b
!file.txtExceptionDo not ignore file.txt
[abc].txta.txt, b.txt, c.txtCharacter class
file?.txtfile1.txt, fileA.txtAny 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
# 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?

TypeGlobalLocal (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:

  1. Is the file correctly named .gitignore (with the dot)?
  2. Are there any trailing spaces at the end of lines?
  3. 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-branch or BFG Repo-Cleaner.

🔝 Back to table of contents



7 - Best practices



✅ Do

  1. Create the .gitignore at the start of the project
  2. Use templates (gitignore.io)
  3. Never commit secrets
  4. Document unusual ignored files

❌ Avoid

  1. Ignoring essential files (package.json, requirements.txt)
  2. Ignoring the .gitignore itself
  3. Ignoring shared config files (.editorconfig)
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/

🔝 Back to table of contents