Sikau

Git & GitHub · Day 5 · about 20 min

What not to commit

.gitignore, secrets and generated files.

A key on the internet

Bots scan GitHub around the clock for passwords and API keys. A key pushed by accident is often abused within minutes. The fix starts before you commit: tell Git which files to ignore.

.gitignore

git init
echo "node_modules/" > .gitignore
echo ".env" >> .gitignore
echo "*.log" >> .gitignore
echo "API_KEY=abc123" > .env
echo "error" > debug.log
touch node_modules/react.js
echo "<h1>Hi</h1>" > index.html
git status
git add .
git commit -m "Add page and ignore rules"
  • Ignore: secrets (.env), dependencies (node_modules/), build output (dist/), logs and OS junk (.DS_Store).
  • Commit the .gitignore itself so the whole team shares the rules.
  • Already committed a file? git rm --cached file stops tracking it but keeps it on disk.
  • Leaked a secret? Revoke it immediately — it stays in history even after you delete it.

The rest of this lesson

  • If a secret leaksexplain
  • Writing ignore patternsexplain
  • Ignored or not?predict
  • Which files are ignored?predict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 18 lessons of Git & GitHub