Git disasters · Chapter 1 of 14

reset --hard ate staged work

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
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

The lesson

By the end of this case

You can tell what a hard reset really throws away, dig staged work back out of Git's object store with git fsck, and give the team a reset that keeps a copy.

The ticket

TAP-911: Loyalty points work gone before the launch. Assigned to you by Kabir. Priority: high. Since: 7 pm today.

Bunty, in the dev channel at 7:04 pm: "i had the whole loyalty points thing ready. loyalty.js, the points column in prices.csv, all staged. there was one debug line in orders.js so i did reset --hard to get rid of it and now EVERYTHING is gone. meera wants it live tomorrow morning"

His terminal, scrolled back:

$ git reset --hard
HEAD is now at 2bfd0a1 Add the order total
$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Kabir: "Nothing was committed, so the reflog will not help this time. But he said staged. Find what Git kept when he staged it, put his work back on main, and give him something safer to type next time. It is his repository in your terminal."

What a hard reset throws away

git reset --hard does three things: it moves the branch (here to where it already was), makes the index match that commit, and makes every tracked file match it too. Staged changes and unstaged changes to tracked files are overwritten. Nothing asks "are you sure?".

What it does not do is delete objects. And git add is not only a note in the index: the moment a file is staged, Git writes its content into .git/objects as a blob. Staging the same file twice writes two blobs. After the reset nothing points to them any more. They are dangling, but they are still there until git gc prunes them, about two weeks later by default.

So the reflog cannot help (it records where branches and HEAD pointed, and these blobs were never in a commit), but the object store can.

Asking Git what nothing points to

On a tiny notes repository, a staged recipe meets a hard reset:

$ git reset --hard
HEAD is now at 5624724 Start a todo list
$ git fsck --lost-found
dangling blob e7d7940a7b7e4b43e66a2fc3d29721603d8b5529
$ ls .git/lost-found/other
e7d7940a7b7e4b43e66a2fc3d29721603d8b5529
$ git cat-file -p e7d7940
Ginger, cardamom, two minutes of boiling

git fsck checks the object store; --lost-found also copies every dangling blob into .git/lost-found/other/, named by its hash. A blob is only content: it has no file name. You tell blobs apart by reading them, and you put one back by writing its content to the right path (git cat-file -p <hash> > recipe.txt, or copying the file out of lost-found). When Bunty staged a file twice, there are two candidates: the content tells you which is the later one.

Changes that were never staged are not in any blob. Those are truly gone, which is why "stage it or stash it" is the habit worth teaching.

A reset that keeps a copy

git stash push --include-untracked puts staged changes, unstaged changes and untracked files away as one entry and leaves the tree clean, which is what Bunty wanted from the reset:

$ git stash push --include-untracked -m "before cleaning"
Saved working directory and index state On main: before cleaning
$ git stash list
stash@{0}: On main: before cleaning

git stash apply --index brings everything back, staged files staged. A Git alias whose value starts with ! runs a shell command, so one alias can make that the thing Bunty types.

Your turn

Bunty's repository is open in the terminal (lab reset rebuilds it if things get worse). First find the blob of the final loyalty.js and write its hash into answer.txt. Then put that loyalty.js and his prices.csv back and commit them on main. Last, make git wipe: it must leave a clean tree and save staged, unstaged and untracked work as a new stash entry; the check tries it in a copy of your repository. Then answer Kabir's interview question below.

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 final loyalty.js

    Bunty staged his work, then a hard reset wiped it. Git still has a copy of each file he staged.

    1. Use git fsck to list the objects that nothing points to now. Ask it to copy them out too.

    2. Read each copied file. Two of them are versions of loyalty.js.

    3. Find the final loyalty.js. It has the line "Double points on Fridays".

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

    To pass: answer.txt has the hash of the final loyalty.js blob, and no other hash. A short hash of 7 characters or more is fine.

  2. Task 2

    Recover: put Bunty's work back on main

    Put Bunty's final work back and commit it, so it is safe.

    1. Write the final loyalty.js blob back into loyalty.js.

    2. Write the prices.csv blob with the points column back into prices.csv.

    3. Stage both files and commit them on main.

    To pass: main has a new commit. Its loyalty.js and prices.csv are exactly as Bunty last staged them. Do not change a line. The other loyalty.js blob is an old draft: do not use it.

  3. Task 3

    Guard it: git wipe, a reset that keeps a copy

    Make a safe command for Bunty, so a reset never loses work again.

    1. Make a Git alias called wipe. Put it in this repository's config or in your global config.

    2. git wipe must save all the work in one new stash entry: staged changes, unstaged changes and untracked files.

    3. After git wipe, git status must show no changes.

    4. Try it: make some changes, run git wipe, then list the stashes.

    To pass: the Check tries git wipe in a copy of your repository. It stages a new file, changes menu.txt and makes an untracked file. After git wipe there must be no changes and one new stash entry. Applying that stash must bring all three back.

What you will learn

  • See what happened: find the final loyalty.js
  • Recover: put Bunty's work back on main
  • Guard it: git wipe, a reset that keeps a copy

More in Lost and found

Build "reset --hard ate staged work" 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.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.