Git without fear · Chapter 27 of 28

I deleted the wrong branch

By the end of this chapter you can bring back a branch you deleted, and delete branches the safe way so it does not happen again. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Detective work
  • 3 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 was tidying up branches. Anjali's old-banner is merged into main, so he meant to delete it. He typed loyalty-card instead, with a capital -D "because the small d complained", and then carried on with a commit on main. His terminal still shows it:

$ git branch -D loyalty-card
Deleted branch loyalty-card (was cfbb5c0).

That branch held three commits of loyalty card work (the rules, a free tenth chai, stamps on the bill), on no other branch and never pushed. Meera wants the cards printed this week. GitHub has no copy, git log --all no longer shows the commits, and Git has no "undelete branch" command:

git switch loyalty-card
fatal: invalid reference: loyalty-card

Last chapter the reflog found commits made in detached HEAD. Here the branch's own reflog was deleted with it, so you need to read HEAD's reflog to find where the branch ended. And the real lesson is the small d that complained.

What you build: 3 tasks

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

  1. Task 1

    Bring loyalty-card back with its three commits

    Bunty deleted the branch loyalty-card by mistake. Its commits still exist. Bring the branch back.

    1. Run git reflog. Find the line where Bunty moved from loyalty-card to main.

    2. The line just below it is his last commit on loyalty-card.

    3. Make a branch called loyalty-card at that commit.

    4. Show the history of loyalty-card to check it. Do not merge it into main.

    To pass: loyalty-card has exactly these commits, newest first: Show stamps on the bill, Give every tenth chai free, Add the loyalty card rules, Add the Diwali banner, Add the menu. main is unchanged: Add the weekend special, Add the Diwali banner, Add the menu.

  2. Task 2 · interview question

    Interview: delete the merged old-banner the safe way

    Interview question: how do you delete a branch safely? Delete old-banner, the branch Bunty meant to delete.

    1. old-banner is merged into main. You can list the merged branches to check.

    2. Delete it with the safe delete: git branch -d old-banner.

    3. Never use -D or --force on old-banner.

    To pass: old-banner is gone, and git branch -d old-banner (or --delete) worked in the chapter folder. If you typed git branch with -D or --force for old-banner even once, this task fails, even if the command failed. Then run lab reset.

  3. Task 3 · interview question

    Interview: why -d would have saved loyalty-card

    Interview question: why does git branch -d refuse to delete loyalty-card?

    1. Bring loyalty-card back first (task 1).

    2. Run git branch -d loyalty-card. It is safe: -d does not delete this branch.

    3. Read Git's error. It gives the reason.

    4. Write the reason into answer.txt, in one sentence.

    To pass: answer.txt gives Git's reason, with Git's own word for what loyalty-card is not.

What you will learn

  • A branch is only a name: deleting it removes the name, not the commits.
  • Finding a deleted branch's tip in HEAD's reflog, and putting the name back on it.
  • -d and -D: the safe delete that refuses unmerged work, and the forced one that does not ask.

More in Detective work

Build "I deleted the wrong branch" 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.