Safety & Recovery · 7 min read · Beginner
.gitignore & Secrets Hygiene
Keep build artifacts, node_modules, and .env secrets out of Git history using .gitignore and pre-commit checks.
What you'll learn
- Write clean .gitignore rules for dependencies, build folders, and .env files
- Remove an accidentally tracked file from Git without deleting it locally using git rm --cached
- Know why leaked API keys must be rotated immediately even after removing them from Git
A .gitignore file tells Git which untracked paths (like node_modules/, dist/, .env, and .DS_Store) should never be staged or committed.
Untracking a File Without Deleting It
If .env was already committed before you added it to .gitignore, .gitignore alone will not untrack it. Run git rm --cached .env, commit the removal, and rotate any exposed secrets immediately.
Fill-in-the-Blank Exercise
Untrack a Secret File Safely
Remove .env from Git tracking while keeping the local file on disk.
git rm .env
git commit -m "Stop tracking .env"Interactive Git Sandbox
GitVerify Tracked Files Before Commit
main
(main) $ git status
On branch main Untracked files: index.html, styles.css
Check Your Understanding
Score: 0 / 1
1. If an API key was accidentally pushed to a public GitHub repo, what is the most important first action?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.