Team workflows · lesson 6 of 6
Commands and ideas in this lesson
ARevoke firstthe secret is leaked; make it useless
Bgit log -Show anyone finds it again
Cgit filter-reporewrite history to remove it
DPush protectionthe host refuses pushes with secrets

A secret got committed

Last updated

Ana's photo upload script had the family photo site's password typed into it, and she pushed it. (In the sandbox it's fake.) Deleting the line in a new commit doesn't help: history keeps it. In this lesson you'll find the secret the way anyone could, learn the right order of steps, and set things up so it doesn't happen again.

By the end of this lesson you can

  • Explain why a committed secret must be treated as leaked
  • Respond in the right order: revoke and replace first, then clean up
  • Say what git-filter-repo can and can't do once a secret is pushed
  • Keep secrets out with environment variables, .gitignore, hooks and host push protection

Deleted, and found in two commands

$ git commit -am "Read the photo password from the environment"
[main a82bb01] Read the photo password from the environment
 1 file changed, 3 insertions(+), 1 deletion(-)
$ git push
To https://example.com/ana/kusina.git
   3f02b9a..a82bb01  main -> main
  1. a82bb01 HEAD →mainorigin/main Read the photo password from the environment
  2. 3f02b9a Add halo-halo recipe
  3. 07d7601 Add a script to upload photos
  4. 67e435d Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page
  7. d078487 Make the sinigang steps clearer
  8. 145dc1e Add pancit bihon recipe
  9. 52d1f4d Say how much salt goes in the adobo
  10. 1c17fad Add a README

and 3 older commits

1 of 3 Two commits ago, Ana committed upload_photos.py with the password in it, and pushed. She notices, and replaces the line.

Automated scanners search public repositories for keys and passwords within minutes of a push. Assume someone has already seen it.

Assume it's leaked

Once a secret has been pushed, especially to a public repository, treat it as known to strangers. The fix isn't in Git at all:

  1. Revoke and replace it first, where it came from: change the password, delete the key or token and make a new one. That makes the leaked copy useless, whatever happens to the history.
  2. Then check whether it was used, fix the code so secrets come from outside the repository, and prevent the next one.

Removing it from history is the last step, and often not needed once it's revoked.

Put the response in order

0 of 5 placed right

  1. Change the password on the photo site (revoke the old one).

  2. Check the photo site for anything done with the old password since it was pushed.

  3. Change the script to read the new password from an environment variable, and commit that.

  4. Add .env to .gitignore and turn on the host’s secret scanning and push protection.

  5. Decide with the family whether to rewrite history to remove the old password.

Rewriting history, and its limits

If the secret (or a huge file, lesson 7.5) must leave history, git-filter-repo rewrites every commit without it. Git's own documentation recommends it over the older git filter-branch.

Terminal, on your computer
git filter-repo --replace-text replacements.txt
git push --force --all
git push --force --tags

replacements.txt lists the text to replace. But know the limits:

  • Every commit from the first one containing the secret gets a new hash (lesson 6.1). Everyone must clone again; the golden rule (lesson 6.3) is broken on purpose.
  • Copies already made (other people's clones, forks, caches, scanners' records) still have it. Hosts may also keep old commits reachable for a while; GitHub asks you to contact support to clear them.

That's why revoking comes first: rewriting can't un-leak anything.

Keeping secrets out

  • Read secrets from the environment, not the code: os.environ["PHOTO_PASSWORD"] in Python. Locally, keep them in a .env file listed in .gitignore; commit a .env.example with the names but no values.
  • Hooks (lesson 7.3) can scan staged changes for things that look like keys before each commit.
  • Push protection on the host (GitHub, GitLab) refuses a push that contains a recognised secret, and secret scanning alerts you about ones already in the repository. Turn them on.
Git sandbox
  1. Find the commits that added or removed the password with git log -S
  2. Print the old file from history with git show <commit>:upload_photos.py
  3. Search an old commit with git grep FAKE <commit>

Working folder

no changes

Staging area

nothing staged

Repository

12 commits on main

Remote

origin: example.com/ana/kusina.git · in step, as of your last fetch

You are in ~/kusina. Two commits ago, a password went into upload_photos.py, and it has been pushed. (It is fake.)

Spot the trap: rewriting but not revoking

Spot the trapAna

Ana runs git-filter-repo, force-pushes, and checks the host: no trace of the password. She doesn’t change the password, because “it’s gone now”.

What happens if…

Check what you learned

You pushed an API key a minute ago. What’s the first thing to do?
Why isn’t deleting the secret in a new commit enough?
Where should the photo password live instead?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git