Git disasters · Chapter 6 of 14
A secret pushed to GitHub
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Rewriting history
- 3 tasks
- Git
- In the all labs plan
The first 2 chapters of Git disasters are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
See what happened: find the commit that added .env
A .env file with the UPI key reached GitHub's main. A later commit deleted it, but the old commits still have it.
1. Show the history of the file .env only. Two commits changed it.
2. Find the commit that added .env. It is Bunty's "Add UPI payments".
3. Write its hash into answer.txt in the case's folder. Do this before task 2: a rewrite gives commits new hashes.
To pass: answer.txt has the hash of the commit that added .env, from before any rewrite, and no other hash. A short hash of 7 characters or more is fine.
- Task 2
Fix: take .env out of every commit on GitHub
Rewrite main so no commit has .env, then replace GitHub's main.
1. Rewrite every commit on main without .env. Use git filter-branch with an index filter. (git filter-repo is not installed here.)
2. Check that no commit on main has .env any more.
3. Push main to GitHub with --force-with-lease.
To pass: no commit on your main or on GitHub's main has .env or the key's text. GitHub's main still has "Add the menu", "Add UPI payments", "Add the Koramangala outlet" and "Show the UPI QR on the bill", in that order. "Remove the .env file" may go or stay. payments.js must stay as Bunty left it.
- Task 3
Guard it: ignore .env, refuse it in a commit
Stop .env from getting into Git again, in two ways.
1. Add .env to .gitignore. Commit it and push main to GitHub.
2. Write a hook in .git/hooks/pre-commit. Start it with a line like #!/bin/sh.
3. In the hook, list the staged files. If one of them is a .env file, in any folder, print a reason and exit with 1.
4. Make the hook executable.
To pass: the Check tries it in a copy of your repository, on GitHub's main. A new .env must not show in git status. git add -f .env and then a commit must be refused. A normal commit of menu.txt must go through.
What you will learn
- See what happened: find the commit that added .env
- Fix: take .env out of every commit on GitHub
- Guard it: ignore .env, refuse it in a commit
More in Rewriting history
Build "A secret pushed to GitHub" yourself
Bunty is Tapri's new junior developer, and Kabir keeps handing you the Git incidents left behind. Each is a disaster interviews love to ask about. 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 disasters are free. This one comes with the all labs plan.