Git without fear · Chapter 23 of 28

The leaked key

By the end of this chapter you can take a secret out of commits before they are pushed, keep it out for good, and say what to do first when one does reach GitHub. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Teamwork
  • 4 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Bunty has built SMS alerts: "Your chai is ready", sent the moment an order is done. His two commits are not pushed yet, and both of them put .env into the history. That file holds the SMS provider's key (TAPRI_SMS_KEY=fake-sk-not-real-123, fake in this lab). Bots scan GitHub for keys all day; a pushed key is found within minutes, and Meera pays for every SMS someone else sends with it.

Deleting .env in a third commit does not help: Git keeps every commit, and the two old ones still hold the key. Adding .env to .gitignore does not help either, because Git keeps tracking a file it already tracks. The key has to leave the history itself.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Take the key out of every commit, before it reaches GitHub

    Bunty's last two commits have .env with the SMS key in them. They are not on GitHub yet.

    1. Show which commits changed .env. Both of Bunty's commits did.

    2. A new commit that deletes .env is not enough. The key stays in the two commits below it.

    3. Undo Bunty's two commits and keep all their changes staged (the soft way from chapter 9).

    4. Take .env out of the staging area, keep the file on your disk, and commit the rest.

    To pass: no commit on any branch, tag or stash, here or on GitHub, has .env or the key fake-sk-not-real-123. sms.js on main still uses TAPRI_SMS_SENDER.

  2. Task 2

    Ignore .env, keep it on your laptop, commit a .env.example

    The app still needs .env on your laptop, but Git must never commit it again.

    1. Untrack .env. Do not delete it: it must still have the key.

    2. Add a line for .env to the .gitignore file that is already there.

    3. Make a file called .env.example with the setting names and no values, like TAPRI_SMS_KEY= on its own line.

    4. Commit .gitignore and .env.example on main, in the same commit as the SMS work.

    To pass: .env is on your disk with the key, Git ignores it, the .gitignore on main lists .env, and the .env.example on main names TAPRI_SMS_KEY without the key.

  3. Task 3

    Push the SMS work to GitHub

    Send the clean SMS work to GitHub.

    1. Check once more that no commit on any branch changes .env.

    2. Push main with a plain git push. No force is needed: GitHub never had Bunty's old commits.

    3. Show the status. It should say your branch is up to date with origin/main.

    To pass: GitHub's main is the same commit as your main. It has the SMS work (sms.js with the sender, orders.js that sends "Your chai is ready") and Meera's commit "Add the menu and the order counter".

  4. Task 4 · interview question

    Interview: a key reached GitHub. What do you do first?

    Interview question: a key is already on GitHub. What do you do first?

    1. Think about who may already have a copy of the key.

    2. Rewriting history cannot take the key back from them.

    3. Write one sentence into answer.txt: what must happen to the old key, and what replaces it.

    To pass: answer.txt says what you do with the key itself, not with the Git history.

What you will learn

  • A secret lives in every commit that has it: deleting the file later leaves the old snapshots as they were.
  • Rewriting unpushed commits: undo them, keep the work, take .env out, commit again.
  • .gitignore and .env.example: the real file stays on your laptop, the team gets the names without the values.
  • Rotating a key: once a key is public, changing it comes first.

More in Teamwork

Build The leaked key yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.