Git Ignore

1 min

Table of contents

  1. Why .gitignore?
  2. Quick Creation
  3. Common Rules
  4. Test and Verification

1. Why .gitignore?

Problem: Git wants to track ALL your files, even the useless ones:

  • Temporary files (.tmp)
  • Build folders (node_modules/)
  • Sensitive files (.env)
  • System cache (.DS_Store)

Solution: .gitignore

  • List of files/folders to ignore
  • Git acts as if they did not exist

Back to table of contents

2. Quick Creation

bash
mkdir demo-gitignore && cd demo-gitignore
git init

# Create test files
echo "print('app')" > app.py
echo "SECRET_KEY=123456" > .env
mkdir temp && echo "cache" > temp/cache.tmp
mkdir logs && echo "error logs" > logs/app.log

# Without .gitignore, Git wants to track everything
git status      # See all the files

# Create .gitignore
cat > .gitignore << EOF
# Sensitive files
.env
*.secret

# Temporary folders
temp/
logs/
build/

# System files
.DS_Store
*.tmp
EOF

# Now Git ignores these files
git status      # Cleaner!

Back to table of contents

3. Common Rules

Useful patterns:

PatternIgnores
*.logAll .log files
temp/The entire temp folder
.env*All files starting with .env
!important.logException: keep this file
build/**Everything in build recursively

Ready-to-use templates:

  • Python: __pycache__/, *.pyc, .env
  • Node.js: node_modules/, npm-debug.log
  • Java: *.class, target/

Back to table of contents

4. Test and Verification

bash
# Check whether a file is ignored
git check-ignore -v .env            # If ignored: displays the rule
git check-ignore logs/app.log       # If ignored: nothing displayed

# Force-add an ignored file (if really needed)
git add -f fichier-ignore.tmp

# See all non-ignored files
git ls-files

Golden rule: Create your .gitignore at the START of the project!

Back to table of contents