Practical guideTested1 min read
.gitignore basics: keep secrets and junk out of Git
Advertisement
Basics
Create a .gitignore file in the project root and list what Git should skip:
.env
__pycache__/Important: it only works on files Git does not track yet
If a secret was already committed, ignoring it does not remove it from history. The old commits still contain it, so rotate the secret (create a new key) and treat the old one as exposed.
Check why a file is ignored
git check-ignore -v .env .
Common mistakes
- Putting the rule in
.gitignoreafter the first commit and assuming the file is gone from the repo. - Committing
.envwithgit add -f, which overrides the ignore rule.
Can I keep a .env.example in the repo?
Yes. Ignore .env, commit .env.example with fake values so others know the names.
Does .gitignore hide files from GitHub?
It keeps them out of commits. Files already pushed stay visible in history.
Source: https://git-scm.com/docs/gitignore
Full kit: Git Survival Kit