How to protect your Git repositories: keys, secrets and leaked tokens

Most code leaks are not clever attacks. They are commits. My checklist for SSH keys, secrets and API keys, and a step-by-step plan for the day a token leaks.

By Laercio Borges

· 5 min read

Most code leaks I have heard about were not clever attacks. They were commits. A .env file pushed on a Friday. An old public repository with a client's database password in a config file. A personal access token pasted into a script "just for a test".

Security for repositories is mostly about a few habits and a plan for the day something goes wrong. Here are mine.

Start private, and make public a decision

Every repository should start private. Making code public should be a choice someone makes on purpose, for that repository, on that day.

Once a year, list every public repository you own and ask one question about each: should a stranger be able to read this? You will probably find at least one that should not be public anymore.

In GitMoon, new repositories are private by default. A repository becomes public only when its owner chooses that, and the Explore feed shows only public repositories. Nothing appears there because of a default.

SSH keys: one per device, with a passphrase

SSH keys are the keys to your code. Treat them like house keys.

  • Use ed25519. It is the modern default and the keys are short.
  • Use a passphrase, and let ssh-agent remember it during your session.
  • One key per device. If your laptop is stolen, you remove one key, not the key you also use on your desktop and your server.
  • Remove old keys. If you do not know which device a key belongs to, remove it.
ssh-keygen -t ed25519 -C "ana@laptop-2026" -f ~/.ssh/id_ed25519_laptop
ssh-keygen -lf ~/.ssh/id_ed25519_laptop.pub

The second command prints the key's SHA256 fingerprint. When you add a public key to a Git host, compare the fingerprint the host shows with this one. GitMoon shows the SHA256 fingerprint of every SSH key you add, in the same format, so the check is easy. Never upload or paste the private key, the file without .pub.

Keep secrets out of the repository

A secret in a repository is in every clone, every fork and every backup. Deleting the file in a new commit does not remove it from history.

The basics:

  • Put .env, *.pem and *.key in .gitignore before the first commit.
  • Commit a .env.example with names and fake values, never real ones.
  • Read your configuration from environment variables or from a secret manager.
  • Run a secret scanner such as gitleaks in a pre-commit hook, and again in CI.

Before each commit, look at what you are about to commit:

git diff --cached
git diff --cached --name-only

It is the cheapest security check there is.

API keys: small, named, and with an end date

Most API keys have more power than the task needs, and they live forever. Both are fixable.

  • One key per use. A key for the deploy script, another for the monitoring job. When one leaks, you revoke one key and nothing else stops.
  • Least privilege. If the provider supports scopes or restricted keys, give each key only what its task needs.
  • Expiry dates. A key that expires in 90 days is a key that cannot leak forever.
  • Names that explain. "deploy-prod-2026-12" tells you what breaks if you revoke it. "key1" does not.

This is how GitMoon API keys work too. A new key is shown to you once. GitMoon stores only a SHA-256 hash of it, plus a short preview so you can recognize it in the list. You give each key a name, and you can revoke it at any time. If you lose a key, GitMoon cannot show it again, because it does not have it. You create a new one.

When a token leaks: the first hour

It will happen to someone on your team one day. Have the steps written down before it does.

1. Revoke or rotate the key first. Do this before you clean anything. Automated scanners look for keys in public code, so assume the key was found within minutes.

2. Check whether it was used. Most providers have logs. Look for requests you do not recognize between the time of the commit and the time you revoked the key.

3. Find every place it appeared. Search the full history, not only the latest files:

git log -p --all -S 'sk_live_' -- .

Replace sk_live_ with a part of the leaked value or its prefix.

4. Remove it from history, if the repository matters. git filter-repo is the tool the Git project recommends for this:

git filter-repo --path config/secrets.yml --invert-paths
git push --force-with-lease --all

Everyone who has a clone must then clone again, because their copy still has the old history.

5. Assume copies exist. Forks, caches, CI logs and other people's clones may still have the old value. That is why step 1 comes first: once the key is revoked, the old copies do not matter.

6. Write five lines about it. What leaked, when, how it was found, what you changed. Not to blame anyone, but so the next person knows what to check.

Protect the accounts, not only the code

Your repositories are only as safe as the accounts that can open them.

  • Turn on two-factor authentication on your Git host and on your email. Your email can reset everything else.
  • Remove people who left the team on their last day, not when someone remembers.
  • If you sign in with Google, the security of your Google account is the security of your code. GitMoon supports Google sign-in, so the two-factor setting on your Google account protects your GitMoon login too.

A short checklist

  1. Every repository private unless someone chose otherwise.
  2. A yearly review of public repositories.
  3. One ed25519 SSH key per device, with a passphrase, fingerprints checked.
  4. .gitignore and .env.example in every project from day one.
  5. A secret scanner in a pre-commit hook and in CI.
  6. API keys with one purpose, few permissions and an expiry date.
  7. A written plan for a leaked token, starting with "revoke first".
  8. Two-factor authentication on the Git host and on email.

If you are moving hosts, this is a good moment to go through the list. I covered the move itself in moving from GitLab or GitHub to GitMoon, and the daily side of this in Git habits that keep a team out of trouble.

FAQ

Is a private repository enough to keep secrets safe?

No. A private repository limits who can read it today, but everyone with access, every clone and every backup still has the secret. Keep secrets out of Git and use environment variables or a secret manager.

If I delete a file with a secret in a new commit, is the secret gone?

No. The secret is still in the history of the repository. Revoke or rotate it first, then remove it from history with a tool such as git filter-repo if the repository matters.

What should I do first when an API key leaks?

Revoke or rotate the key at the provider immediately, before you clean the code. Then check the provider's logs for use you do not recognize.

How does GitMoon store API keys?

GitMoon shows a new API key once and stores only a SHA-256 hash of it, plus a short preview. You can revoke a key at any time.

Try GitMoon

The Starter plan is free. It has 5 repositories, private by default, and 2,500 AI tokens each month.

Keep reading