More Git answers

by Danny
Published 3 min read

Rotate the leaked secret first, because rewriting history cannot take it back. Then remove it from every commit with git filter-repo and force push, knowing clones and forks may keep it.

The short answer

#

Revoke or rotate the secret straight away. Assume it is compromised the moment it was pushed. Then, if you also want it out of the history, rewrite every commit with git filter-repo and force push.


_10
# 1. Rotate the key with the provider first
_10
# 2. In a fresh clone, replace the secret in all history
_10
echo 'sk_live_abc123==>REMOVED' > ../replacements.txt
_10
git filter-repo --replace-text ../replacements.txt
_10
# 3. Re-add the remote and overwrite it
_10
git remote add origin [email protected]:you/project.git
_10
git push --force --all origin
_10
git push --force --tags origin

Why it happens

#

Every commit stores a full snapshot of your files. Deleting the secret in a new commit only removes it from the latest snapshot; every older commit still contains it, and anyone with the repository can read it with git show or git log -p.

Once a commit is pushed, you also lose control of the copies. Other developers have cloned it, CI systems have checked it out, forks may exist, and automated scanners watch public repositories for keys within minutes. Rewriting history cleans the repository you control, but it cannot reach those copies. That is why rotating the secret always comes first: a revoked key is harmless wherever it ends up.

How to fix it

#

Rotate the secret

Go to the provider (your cloud console, payment provider, database host and so on), revoke the leaked credential and issue a new one. Check the provider's logs for any use you do not recognise. Put the new value in an environment variable or secret manager, not in the code.

If you have not pushed yet

If the commit only exists locally, you do not need to rewrite anything shared. Remove the file or value, then amend the commit.


_10
git rm --cached .env
_10
echo ".env" >> .gitignore
_10
git add .gitignore
_10
git commit --amend --no-edit

If the secret is in an older local commit, use git rebase -i to edit that commit, or use git filter-repo as below before pushing. Rotating is still wise if there is any chance it left your machine.

Remove it from all history with git filter-repo

git filter-repo is the tool the Git project recommends in place of the older git filter-branch. Install it from your package manager (for example brew install git-filter-repo or pip install git-filter-repo).

Work in a fresh clone. By default filter-repo refuses to run in a repository that is not a fresh clone, so you do not destroy local work by mistake.


_10
git clone [email protected]:you/project.git project-clean
_10
cd project-clean

To replace the secret text wherever it appears, list it in a file. Each line is a literal string, and ==> sets the replacement. Keep this file outside the repository.


_10
printf 'sk_live_abc123==>REMOVED\n' > ../replacements.txt
_10
git filter-repo --replace-text ../replacements.txt

To remove a whole file from every commit instead, use --invert-paths.


_10
git filter-repo --invert-paths --path .env

The rewrite changes the hash of every affected commit and everything after it. filter-repo also removes the origin remote as a safety measure, so add it back before pushing.

Force push and tell everyone


_10
git remote add origin [email protected]:you/project.git
_10
git push --force --all origin
_10
git push --force --tags origin

Branch protection may block force pushes, so an admin may need to allow it briefly. Then ask everyone to delete their old clones and clone again. If someone merges or pushes from an old clone, the secret comes straight back.

On hosted platforms, old commits can still be reachable through pull request refs or caches. On GitHub, for example, you may need to contact support to purge them. Forks are separate repositories and keep the old history.

Stop it happening again

  • Add .env and similar files to .gitignore before you create them.
  • Turn on secret scanning and push protection if your host offers it.
  • Run a scanner such as gitleaks in a pre-commit hook or in CI.

Common pitfalls

#
  • Rewriting history but not rotating. The key is already exposed. Rewriting alone gives a false sense of safety.
  • Deleting the file in a new commit and stopping there. The secret is still in older commits.
  • Keeping the replacements file in the repo. It contains the secret. Store it outside and delete it afterwards.
  • Teammates pushing from old clones. One push undoes the whole clean-up. Make sure everyone reclones.
  • Forgetting tags and other branches. Push with --all and --tags, or the secret survives there.
#

Was this resource helpful?