Chapter 5: Ignoring Files in Git

Sometimes you have files in your project that Git should not track. These might be:

# You can tell Git to ignore them using a file called .gitignore

# This is a comment
.env           # Ignore env file
*.log          # Ignore ALL log files
temp/          # Ignore a folder
!keep.log      # But do not ignore this specific log file

# Let’s break it down with real examples πŸ‘‡

πŸ“ .gitignore Rules Explained Simply:

  • # ➜ Comment line. Git will ignore this line completely.
  • *.log ➜ Match all files that end with `.log`.
    🧠 Example: debug.log, error.log
  • .env ➜ Ignore a specific file named `.env`.
    🧠 Often used for secrets and API keys.
  • temp/ ➜ Ignore everything inside the temp folder.
    🧠 Example: temp/file.txt
  • !keep.log ➜ Do not ignore keep.log even if matched earlier.
    🧠 Useful to whitelist specific files.
  • /notes.txt ➜ Ignore notes.txt only if it's in the root folder.
    🧠 Won’t ignore src/notes.txt.

# Let's practice:

# Bonus Tip:

If you accidentally committed a file before ignoring it, use this:

git rm --cached filename

Then add the file to `.gitignore` so it won't be tracked next time.