All guides

A secret leaked to Git: what to do in the first hour

GitHub's guidance is that removing a secret from the codebase is not sufficient. The most important step is revoking it with its provider. History cleanup comes after that, and GitHub notes that it may not be warranted once the secret is revoked.

A laptop on a desk beside stacks of books and a lamp

Why the order matters

Once a secret has been committed to a repository, GitHub says you should consider it compromised. A hardcoded secret becomes part of the Git history and remains accessible even after it is removed from the latest commit. Search engines and specialized scanning services index public repositories, exposed secrets included.

A revoked or rotated secret gives no further access. GitHub calls cleaning the Git history a destructive and disruptive process. So the order is:

  1. Revoke and rotate the secret.
  2. Check the logs for unauthorized access.
  3. Clean the history, if it is still warranted.
  4. Add scanning, and document the incident.

1. Revoke and rotate the secret

Determine the secret type and its provider, and identify the secret's owner. GitHub's tutorial suggests the repository's CODEOWNERS file and git log -S, which searches the commit history to identify who committed the secret.

Then revoke the secret with its provider and generate a new one if needed. Where downtime is a concern, GitHub's tutorial allows a different order: create a new secret that has the same permissions, switch the application to it, then revoke the old secret.

Update every service that used the old secret, and test that each one works with the new secret. For a GitHub personal access token, delete the compromised token and create a new token.

2. Check the usage logs

  • GitHub's audit logs. Review them for events relating to the secret and its usage. The security log covers a personal account. In the organization and enterprise audit logs, you can search for events related to one access token. Access to these logs depends on your role.
  • The provider's audit logs. GitHub's example is AWS: look in the CloudTrail logs for attempts to gain access with the leaked secret that nobody authorized.

3. Clean the history

GitHub says that once the secret is revoked or rotated, going through the extra steps to rewrite the history may not be warranted. It lists these side effects of a rewrite:

  • The hashes change for the commits that introduced the data and for all commits that came after.
  • The diff view of closed pull requests is broken.
  • Signatures on commits and tags are lost.
  • Other developers' work is at risk if they keep updating the affected branches.
  • A colleague with an old clone who runs git pull and then git push brings the data back.

Weigh those effects against your compliance or security obligations, together with the repository's security leads.

GitHub's procedure uses git-filter-repo, version 2.47 or later, in a clone of the repository:

Shell

git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
cd YOUR-REPOSITORY
git-filter-repo --sensitive-data-removal --invert-paths --path <path-to-file>
git push --force --mirror origin

To remove text from files that stay, list the text in a file and run this in place of the third command:

Shell

git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt

If any other ref fails to push, check whether branch protection is turned on for that branch. GitHub's page says to turn it off temporarily and redo the push. Collaborators must rebase, and must not merge, any branches they created off the old history. One merge commit could reintroduce the history you purged.

What a force push leaves behind

After the force push, GitHub lists three places where the commits may still be accessible:

  • Clones and forks. You cannot remove sensitive data from other users' clones. If the commit exists in a fork, it continues to be accessible there, and you need to coordinate with the fork's owner.
  • Cached views. The commits can be reached directly through their SHA-1 hashes in cached views on GitHub.
  • Pull requests. The push fails for refs that start with refs/pull/, because GitHub marks those as read-only.

Count the affected pull requests before you push:

Shell

grep -c '^refs/pull/.*/head$' .git/filter-repo/changed-refs

Then contact GitHub Support with the owner and repository name, the number of affected pull requests and the First Changed Commit(s) that git-filter-repo reported. Support can dereference or delete the affected pull requests, run a garbage collection on the server and remove cached views.

Support assists only where it determines that the risk cannot be mitigated by rotating the affected credentials.

Scanning and a written record

  • Prevent the next push. Ensure that push protection is enabled for the repository, and that "Push protection for users" is enabled on your personal account. GitHub also suggests a pre-commit hook that checks for sensitive data, with a tool such as gitleaks.
  • Fix the cause. When the data sat in a file Git should never track, put that filename in .gitignore. Avoid hardcoding secrets in code. GitHub's page names environment variables and secret management services as the way to inject secrets at runtime.
  • Close the alert. Secret scanning does not automatically close an alert when the token is removed from the repository. Close it with "Close as" and mark it as "Revoked".
  • Document it. Write the incident up where your team keeps such records, a knowledge base or an incident management system. Include how the leak was remediated and what the team learned.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides