Things go wrong
Last updated
Kusina v1.0 is out, and three things go wrong in one week. The leche flan comes out raw, and nobody knows which of a dozen commits did it. Carla deletes a branch with work she hadn't pushed. And Ana pushes a script with the photo site's password in it. You'll set up each disaster yourself, then recover from it with the tools from lessons 2.7, 7.6 and 8.4.
By the end of this lesson you can
- Find a breaking commit with git bisect run, starting from a release tag
- Recover a deleted branch from the reflog
- Respond to a committed secret in the right order
Disaster 1: the raw leche flan
A week of small fixes lands on main. Then the reunion's leche flan comes out raw. To set this up, run this script in ~/carla-kusina on an up-to-date main. It makes twelve small commits as Carla, and one of them breaks the leche flan. Don't read it closely: the point is to find the bad commit with Git, the way you would in a real project where you didn't write it.
#!/bin/sh # Twelve small commits. One of them breaks a recipe. set -e c() { git commit -qam "$1"; } p() { perl -pi -e "$1" "$2"; } p "s/Serves 4\./Serves 4 to 6./" tinola.md; c "fix: say the tinola serves 4 to 6" p "s/- 3 calamansi/- 4 calamansi/" pancit.md; c "fix: more calamansi for the pancit" p "s/Fry until golden\./Fry in hot oil until golden, about 4 minutes./" lumpia.md; c "fix: give a frying time for lumpia" p "s/8 cloves garlic/10 cloves garlic/" sinangag.md; c "fix: more garlic in the sinangag" p "s/Makes 24\./Makes about 24./" puto.md; c "fix: say the puto count is rough" p "s/steam for 40 minutes/steam for 4 minutes/" leche-flan.md; p "s/5 cups water/5 cups of water/" champorado.md; c "chore: tidy wording in dessert recipes" p "s/about 2 hours/about 2 to 3 hours/" kare-kare.md; c "fix: oxtail can take three hours" p "s/4 cups shaved ice/4 cups finely shaved ice/" halo-halo.md; c "fix: say the ice should be fine" p "s/stirring often\./stirring often, until thick./" arroz-caldo.md; c "fix: say how thick the arroz caldo gets" p "s/Makes 12\./Makes 12. Best eaten warm./" turon.md; c "docs: say turon is best warm" p "s/Serves 8\./Serves 8 to 10./" maja-blanca.md; c "fix: maja blanca serves more" p "s/Leave for an hour\./Leave for an hour, or overnight./" bistek.md; c "fix: the bistek can marinate overnight" echo "Done: twelve commits."
Save it as week.sh outside the repository (in your home folder), and run sh ~/week.sh from ~/carla-kusina. Then git push.
Find it with bisect
You know v1.0 was good (the leche flan was fine at the reunion) and main is bad. A command can tell them apart: the good recipe says steam for 40 minutes.
git bisect start main v1.0 Bisecting: 5 revisions left to test after this (roughly 3 steps) ... git bisect run grep -q "steam for 40 minutes" leche-flan.md running 'grep' '-q' 'steam for 40 minutes' 'leche-flan.md' ... 0c7f336df410215ace80526267417264fd890f49 is the first 'bad' commit ... chore: tidy wording in dessert recipes ... bisect found first 'bad' commit git bisect reset
Read the commit with git show <hash>. Its message says "tidy wording", and the change to the champorado is just that, but the leche flan line is a real change hidden inside it. Fix it with a revert (lesson 2.5) or a new fix: commit that puts 40 minutes back, and push.
(Of course git log -S "steam for 4 minutes" would also find it straight away, once you know what to search for. Bisect is for when you don't.)
Disaster 2: a deleted branch
Carla starts on a branch of new desserts and doesn't push it. Then, tidying up, she deletes the wrong branch. Set it up in ~/carla-kusina:
git switch -c carla-desserts (make buko-pie.md with a recipe of your choice) git add buko-pie.md git commit -m "feat: add buko pie recipe" (make cassava-cake.md) git add cassava-cake.md git commit -m "feat: add cassava cake recipe" git switch main git branch -d carla-desserts error: the branch 'carla-desserts' is not fully merged hint: If you are sure you want to delete it, run 'git branch -D carla-desserts' hint: Disable this message with "git config set advice.forceDeleteBranch false" git branch -D carla-desserts Deleted branch carla-desserts (was 6c5b4a3).
Git warned her (-d refused), and she forced it with -D. The branch was never pushed, so the shared repository can't help.
Get it back
Deleting a branch deletes the label, not the commits (lesson 8.3). They're still in the repository, and the reflog remembers HEAD being on them:
git reflog -4 2d3e4f5 (HEAD -> main, origin/main) HEAD@{0}: checkout: moving from carla-desserts to main 6c5b4a3 HEAD@{1}: commit: feat: add cassava cake recipe 9b8a7c6 HEAD@{2}: commit: feat: add buko pie recipe 2d3e4f5 (HEAD -> main, origin/main) HEAD@{3}: checkout: moving from main to carla-desserts git branch carla-desserts 6c5b4a3 git log --oneline main..carla-desserts 6c5b4a3 feat: add cassava cake recipe 9b8a7c6 feat: add buko pie recipe
Use your own hash, from your reflog or from the "was" in the delete message. The branch is back, both commits on it. Push it this time: git push -u origin carla-desserts.
Unreachable commits are kept for a while (by default at least two weeks) before Git's clean-up may remove them, so don't leave a rescue for long.
Disaster 3: a password pushed
Ana writes a script to upload recipe photos, and puts the photo site's password straight into it. Set it up in ~/ana-kusina, with text that's obviously fake:
PASSWORD = "FAKE-kusina-photo-password-for-the-course" # Uploads recipe photos to the family photo site.
git pull git add upload_photos.py git commit -m "feat: add a script to upload photos" git push
If your shared repository is on a public hosting site, its secret scanning may flag even this; that's fine, and it's the protection from lesson 7.6 doing its job. Use a private repository if you'd rather not.
Respond in order
Play it as if the password were real:
- Revoke it. Write down, as a note to yourself, "Changed the photo site password at (time)". In real life this is first, and it's done on the photo site, not in Git.
- Check for misuse. Note "Checked the photo site's sign-in history".
- Fix the cause. Change the script to read it from the environment, and commit:
import os PASSWORD = os.environ["PHOTO_PASSWORD"] # Uploads recipe photos to the family photo site.
git commit -am "fix: read the photo password from the environment" git push
- Prevent the next one.
.envis already ignored (C.2). Add a.env.examplewith the name and no value (PHOTO_PASSWORD=), commit it, and if you use a host, turn on its secret scanning and push protection. - Decide about history.
git log -S FAKE --onelinestill finds it. With the password changed, it's useless; rewriting shared history would make both cousins clone again. Most teams stop here. If you want to try a rewrite, git-filter-repo is the tool (lesson 7.6); do it on a copy first.
Check what you did
git log --oneline -4 ... git show HEAD:leche-flan.md | grep steam 3. Pour over the caramel and steam for 40 minutes. git branch -a | grep carla-desserts remotes/origin/carla-desserts grep -n PASSWORD upload_photos.py 3:PASSWORD = os.environ["PHOTO_PASSWORD"]
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.
- Git documentation: git-bisect: bisect run and exit codes
- Git documentation: git-reflog
- Git documentation: git-branch: -D, and making a branch at a commit
- GitHub Docs: Removing sensitive data from a repository: revoke first