Ignoring Files

View saved

Create a .gitignore file

List patterns for files Git should leave untracked. Commit .gitignore itself so everyone on the project shares the same rules.

echo '.env' >> .gitignore
echo '__pycache__/' >> .gitignore
echo '*.log' >> .gitignore
git add .gitignore
git commit -m "Ignore env files, caches, and logs"

Use common patterns

  • *.log ignores log files in any folder
  • build/ ignores a directory named build
  • .env ignores a local secrets file
  • !important.log can re-include an exception when needed

Understand already-tracked files

Ignoring only affects untracked files. If Git already tracks a file, remove it from the index without deleting your working copy, then commit.

git rm --cached secrets.txt
git commit -m "Stop tracking secrets.txt"

Check what Git sees

Status should no longer list ignored untracked files. That is the goal: a clean view of real project changes.

git status

Comments

One comment per signed-in account. Comments are saved with this page’s URL.