TutreNews Learn

Undoing and ignoring

Ignoring files

Goal: Keep generated files and secrets out of the repository · 6 min

A .gitignore file lists patterns of files Git should not track:

node_modules/     # a whole folder
*.log             # all files ending in .log
.env              # secrets, API keys
dist/             # build output
!keep.log         # exception: do track this one

Ignore what can be regenerated (dependencies, build output, caches) and what must stay private (passwords, keys). Commit the .gitignore itself so everyone shares it. Note: .gitignore only affects untracked files — a file already committed stays tracked until you run git rm --cached file. A secret that was ever pushed must be considered leaked and rotated.

Worked example: Stop tracking a committed log file

  1. Add the pattern to .gitignore.
  2. Remove the file from the index only (it stays on disk).
  3. Commit both changes.
echo "*.log" >> .gitignore
git rm --cached server.log
git add .gitignore
git commit -m "Stop tracking log files"