Skip to content

Recover a deleted branch

≈ 20 minutesBeginner6 steps · 20 min estimated

Outcome. Restore a force-deleted branch to its exact last commit using the reflog, and explain why that works and when it stops working.

Before you start: Git branches: how branching really works

The duration is an estimate of engaged work, not a timer — nothing here waits. Steps marked "Check" and "Practice" are verified in the browser; "Read" and "Lab" steps are yours to confirm.

  1. Read

    Branches are pointers

    ≈ 4 min

    Read "Branches and commits" through "Creating a branch". The sentence that matters: a branch is a name for one commit.

  2. Read

    Recovering a deleted branch

    ≈ 3 min

    The lesson's own recovery section. Note what `git reflog` lists and what its entries point at.

  3. Predict

    Predict: what does `git branch -D` delete?

    ≈ 1 min

    Answer from the pointer model.

    You run `git branch -D feature` on a branch with three unmerged commits. Immediately afterwards, what has been removed from the repository?
    Why

    Deleting a branch deletes a pointer. The commits remain as objects, reachable through the reflog, until garbage collection prunes unreachable ones (reflog entries expire after 90 days by default).

  4. The check verifies the branch points at the last commit — not merely that a branch with that name exists.

    Scenario: Recover a deleted branch — completes automatically when the simulator's check passes.

  5. Lab

    Do it in a real repository

    ≈ 6 min

    The same recovery in `/tmp`, with real reflog output. Mark this step when your `git log --oneline` shows all three commits.

  6. Check

    Knowledge check: HEAD

    ≈ 1 min

    Detached HEAD and recovery, from the lesson the reflog lives in.

    Completes when the knowledge check on that page has been attempted.

When you are done: Resolve and verify a merge conflict. Recovery handles the "lost" case; conflicts are the other everyday interruption, and the session is the same length.

How did this go?