shellstreak

.gitignore and keeping secrets out of git

Updated · Every command on this page ran in our practice shell

Short answer

List files git should leave alone in .gitignore, one pattern per line (.env, *.log, node_modules/), and commit it. It only affects untracked files: for a file that's already committed, run git rm --cached file and commit. A secret that was ever pushed stays in the history, so revoke and rotate it first.

Some files never belong in a repository: .env files with keys, installed packages like node_modules/, build output and logs. A .gitignore file tells git to leave them alone. It's simple, with one big catch: it only works on files git isn't tracking yet. The examples below ran in a practice shell on a small garden-planning repository.

See what git would commit

git status -s marks untracked files with ??. Anything listed there gets swept into the next commit by git add . or git add -A, including a .env full of keys.

example
learner@shellstreak:~/garden$ git status -s
?? .env
?? debug.log

Ignore files with .gitignore

Put one pattern per line in .gitignore at the top of the repository: a name (.env), a wildcard (*.log) or a folder with a trailing slash (node_modules/). Commit the .gitignore itself so the whole team shares it.

example
learner@shellstreak:~/garden$ echo '.env' >> .gitignore && echo '*.log' >> .gitignore && git status -s
?? .gitignore

Find out which rule ignores a file

git check-ignore -v file prints the .gitignore file, the line number and the pattern that matched. It prints nothing (and fails) when the file isn't ignored.

example
learner@shellstreak:~/garden$ git check-ignore -v debug.log .env
.gitignore:2:*.log	debug.log
.gitignore:1:.env	.env

Stop tracking a file that's already committed

Adding a tracked file to .gitignore changes nothing: git keeps tracking it. git rm --cached file removes it from git but leaves it on disk. Commit that together with the .gitignore line.

example
learner@shellstreak:~/garden$ git rm --cached .env && echo '.env' >> .gitignore && git add .gitignore && git diff --staged --stat
rm '.env'
 .env       | 1 -
 .gitignore | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

The history still has it: git log -S

Untracking a file doesn't remove it from older commits. git log -S text (the pickaxe) lists the commits that added or removed text, so you can see how far back a secret goes.

example
learner@shellstreak:~/garden$ git log -S sk- --oneline
413e41a Stop tracking .env
2958983 Add weather settings

If a key was pushed: rotate it

Once a secret has reached a shared server, assume it's been copied: bots scan public repositories within minutes, and every clone has the history. Revoke the key and issue a new one first. Then load it from the environment or a secret store, ignore the file, and only then consider rewriting history, which needs the whole team's cooperation.

Check yourself

One real exercise from the “Git basics” unit. No account needed.

git status -s offers .env (with an API key) and debug.log. Add the patterns .env and *.log to .gitignore, one per line, then check with git status -s.

What's checked: the output of your last command; the file garden/.gitignore.

Common mistakes

  • Adding an already-tracked file to .gitignore and expecting git to forget it. Run git rm --cached too.

  • Using git rm .env without --cached, which deletes your only copy of the settings.

  • Deleting a leaked key in a new commit and calling it fixed. It's still in the history: rotate it.

  • Running git add -A or git add . without reading git status first, and committing .env or node_modules/.

Questions

Should .gitignore be committed?+

Yes. It's part of the project, so everyone ignores the same files. For files only you create (editor settings, notes), use `.git/info/exclude` or a global ignore file instead.

Why is my .gitignore not working?+

Usually because the file is already tracked. `.gitignore` only affects untracked files: `git rm --cached file`, then commit. `git check-ignore -v file` shows whether a rule matches at all.

How do I share settings without the secrets?+

Commit a `.env.example` with the variable names and fake values, and ignore the real `.env`.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now