Git disasters · Chapter 4 of 14

A force-push wiped a teammate's commits

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Lost and found
  • 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.

  1. Task 1

    See what happened: find the commit GitHub lost

    Bunty force-pushed main from an old clone. GitHub's main lost Ravi's two commits from this morning.

    1. Run git fetch. Git says that GitHub's main was force-updated.

    2. Your repository keeps a history of origin/main. Look at that history.

    3. Find where GitHub's main was just before the forced update. It is Ravi's "Fix the UPI timeout".

    4. Write its hash into answer.txt in the case's folder.

    To pass: answer.txt has the hash of the commit GitHub's main was at before the force-push, and no other hash. A short hash of 7 characters or more is fine.

  2. Task 2

    Recover: Ravi's and Bunty's work, both on GitHub

    Put back Ravi's two commits and keep Bunty's new commit too, with no force.

    1. On main, move main forward to Ravi's last commit. Use his own commits, not copies.

    2. Merge GitHub's current main into your main. This brings in "Add the monsoon specials".

    3. Push main to GitHub with a normal push.

    To pass: GitHub's main has "Add the Koramangala outlet", "Fix the UPI timeout" and "Add the monsoon specials", each exactly once. Ravi's two commits must be his original commits: a cherry-pick makes copies, and copies do not count. upi.js must have Ravi's 15 second timeout, and menu.txt must have Bunty's pepper chai.

  3. Task 3

    Guard it: no push may rewrite main

    Make Git refuse any push that would rewrite main on GitHub.

    1. Write a hook in .git/hooks/pre-push. Start it with a line like #!/bin/sh.

    2. Git gives the hook one line for each branch it pushes. For main, check that GitHub's current commit is an ancestor of the commit you push.

    3. If it is not, or if the push deletes main, print a reason and exit with 1.

    4. Make the hook executable.

    To pass: the Check tries it in a copy of your repository. It rewrites main and runs git push --force: the push must fail, and GitHub's main must not change. Then it pushes a normal new commit on top of GitHub's main: that push must work.

What you will learn

  • See what happened: find the commit GitHub lost
  • Recover: Ravi's and Bunty's work, both on GitHub
  • Guard it: no push may rewrite main

More in Lost and found

Build "A force-push wiped a teammate's commits" 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.