brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git grep over every commit finds a secret that was ever committed

Git
~/tips/tip-git-grep-all-history-secret

git grep -n 'API_KEY' $(git rev-list --all)

Searches all versions of all files in history, not just the current checkout.

Deleting a file with a password in it doesn’t remove it from history. To check whether a string was ever committed, I’d let git grep search every commit.

git grep -n 'API_KEY' $(git rev-list --all)

git rev-list --all lists every commit on every branch and tag, and git grep searches the files of each one. A hit looks like b416670...:config/.env:1:API_KEY=abc: the commit, the path, the line number and the line. So even if the file was removed three commits later, you see where it lived.

To narrow it to one path, add a pathspec after --:

git grep -n 'password' $(git rev-list --all) -- '*.yml'

On a big repository this is slow, and the list of commits can get so long that the shell refuses it with an “argument list too long” error. Then loop through the commits in batches with xargs, or use a dedicated secret scanner.

Finding it is the easy part. If a real key or password shows up, assume it’s leaked once the repository was pushed anywhere. Revoke or rotate it first. Rewriting history afterwards is optional cleanup, because forks, clones and caches may still have the old commits.

Further reading: git-grep documentation, git-rev-list documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link