Git security
Git remembers everything, including the token you deleted. Watch five ways a repository goes wrong, the guard that stops each, and what to do when a secret is already out.
What you'll take away
- Explain why deleting a .env later doesn't help, and stop it with a pre-commit scan
- Rotate a leaked token first, then clean history with
git filter-repo - Protect main at the host: no force-push, a required review, signed commits only
- Pick a fine-grained token: one repo, only the rights it needs, an expiry
The 1-minute version
Tap a point and the picture below plays that step.
0 of the 5 are handled now. The pre-commit hook lives on your machine and --no-verify skips it; the rest live at the host, where they're much harder to skip
You committed a .env with a token and pushed it to a public repo. The next commit deletes the file. Is that enough?
The picture: your laptop's commits stand as a row of houses on the left, each with its change on the roof (red: it holds a token), and the pre-commit check across your path when the scan is on. The push road runs right to the host's walled lot, origin/main inside, one gate in its wall where the host's rule of the step stands; PR #12 waits beside it and production past it. Below, the attacker's street, with an old clone at one end and the service a leaked token opens at the other. The repo and the token are toys.
Compare them all: Git security
The same five things happen to four repositories, each adding guards to the one before. See what still gets through.
Use it when A toy on your own machine nobody else touches
Skip it when Anything shared, or with secrets near it
A model: each threat is paired with its main guard to keep it readable. The setting names differ by host (GitHub, GitLab, Bitbucket), but every one has them. Press any tile to see it large above, with when it fits.
Five threats, five guards
| Threat | Guard | Where |
|---|---|---|
| a token in a commit | .gitignore + pre-commit scan | your machine |
| a force-push over main | no force-push to main | the host |
| merged with nobody looking | required review | the host |
| a commit in someone else's name | signed commits required | the host |
| a token stolen from a laptop | scoped tokens + 2FA | the host |
Leaked? Rotate first
Once a secret is pushed, assume it's taken: bots watch public pushes. Revoke it and make a new one before anything else. Cleaning history comes after, and it can't reach the clones and forks that already have it.
Local guards help; host guards decide
A pre-commit hook catches most mistakes before they exist, but it lives on one machine and --no-verify skips it. Branch protection, required reviews, push protection and signature rules live at the host, where everyone meets them.
In real code
#!/bin/sh
# A token committed, then "deleted": still in history. Then the same mistake stopped twice (.gitignore, a pre-commit
# hook), what --no-verify does to the hook, and what rewriting history can't reach (a clone made before).
# The token is made up at run time (random letters behind GitHub's classic-token prefix): no real secret is in here.
# History is rewritten with git filter-branch only because it ships with git; git's own docs point to git filter-repo.
# Run: sh secrets.sh (needs git; works in a throwaway folder, then deletes it)
set -eu
export GIT_AUTHOR_NAME=Mai GIT_AUTHOR_EMAIL=mai@example.com GIT_COMMITTER_NAME=Mai GIT_COMMITTER_EMAIL=mai@example.com
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 FILTER_BRANCH_SQUELCH_WARNING=1
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
token="ghp""_$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 36)"
in_history() { git log --all -S "$token" --format=%s | wc -l | tr -d ' '; }
git init -q -b main "$tmp/shop" && cd "$tmp/shop"
echo "# shop" > README.md && git add . && git commit -qm "readme"
echo "console.log('hi')" > app.js && git add . && git commit -qm "app"
echo "GITHUB_TOKEN=$token" > .env && git add . && git commit -qm "add .env"
git rm -q .env && git commit -qm "remove .env"
[ ! -e .env ] && [ "$(git show HEAD~1:.env)" = "GITHUB_TOKEN=$token" ]
echo "deleted file, still in history: $(in_history) commits"
git clone -q "$tmp/shop" "$tmp/old-clone"
# 1. .gitignore first: git won't stage the file at all
echo ".env" > .gitignore && git add .gitignore && git commit -qm "ignore .env"
echo "GITHUB_TOKEN=$token" > .env
if git add .env 2>/dev/null; then echo "unexpected: .env staged"; exit 1; fi
printf '.gitignore: git add refused · '
# 2. a pre-commit hook that refuses token-shaped text (real scanners such as gitleaks know hundreds of shapes)
cat > .git/hooks/pre-commit <<'HOOK'
#!/bin/sh
if git diff --cached -U0 | grep '^+' | grep -Eq '(ghp|gho|ghs|ghu)_[A-Za-z0-9]{36}|AKIA[0-9A-Z]{16}'; then
echo "pre-commit: refused, the staged changes contain something that looks like a token" >&2
exit 1
fi
HOOK
chmod +x .git/hooks/pre-commit
echo "const token = '$token'" > config.js && git add config.js
if git commit -qm "config" 2>/dev/null; then echo "unexpected: commit went through"; exit 1; fi
printf 'pre-commit hook: commit refused · '
git commit -q --no-verify -m "config" && printf '%s' "--no-verify: went through · "
git reset -q --hard HEAD~1
# 3. rewrite history to take .env out of every commit, and drop what still points at the old ones
git filter-branch --index-filter 'git rm -q --cached --ignore-unmatch .env' -- --all > /dev/null 2>&1
git for-each-ref --format='%(refname)' refs/original | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all && git gc -q --prune=now
here=$(in_history); there=$(cd "$tmp/old-clone" && in_history)
[ "$here" = 0 ] && [ "$there" = 2 ]
echo "after rewriting history: $here here, an old clone still has $there, so revoke the token first"
sh secrets.sh (needs git; runs in throwaway repositories, then deletes them)It printed:deleted file, still in history: 2 commits .gitignore: git add refused · pre-commit hook: commit refused · --no-verify: went through · after rewriting history: 0 here, an old clone still has 2, so revoke the token first
Real git in a throwaway repo: a made-up token committed and deleted (still in 2 commits), .gitignore refusing the file, a pre-commit hook refusing the commit, --no-verify walking past it, and a history rewrite that leaves an old clone holding the token. Really run; below is what it printed.
Common questions
I deleted the .env in the next commit. Am I safe?
No. Git keeps every commit whole, so the token is still in history and every clone carries it. If the repo is public, bots are already reading every push.
I pushed a secret. What do I do first?
Revoke it and make a new one before anything else; assume it's taken. Then clean history with git filter-repo and have everyone clone again. Rewriting history can't reach the clones and forks that already have it.
How do I keep secrets out of commits in the first place?
Put .env in .gitignore from the first commit, and add a pre-commit hook that scans for token-shaped text. The hook lives on one machine and --no-verify skips it, so turn on the host's push protection too.
How do I stop a force-push from wiping main?
Branch protection at the host: main refuses force-pushes and only moves through PRs. If you must force-push your own branch, use --force-with-lease, which refuses if the remote branch has moved since you last fetched.
Why require a review before merging to main?
So nothing reaches main, and production, without anyone seeing it. Main takes only PRs another person approved with CI passing, so a bug or something stranger meets at least one more pair of eyes.
Can someone commit in my name?
Yes. A git author's name and email are just text; anyone can set user.name to anything. Sign commits with an SSH or GPG key: the host shows Verified for a valid signature, and main can refuse commits without one.
What kind of access token should I use?
A fine-grained token: one repo, only the permissions it needs, and an expiry. If it leaks, it reaches that one repo; a classic token with full repo scope reaches every repo you can. And turn on 2FA for the account.
Next lesson
Supply chain attacksLevel: Intermediate
Your own repo is guarded; next, the code your dependencies run at install, in CI, where your tokens may be waiting.