Interview Q&A / Git & GitHub

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How can you recover a branch that you deleted by mistake?

SHORT ANSWER

Find the branch's last commit hash with git reflog, then recreate it with git branch name hash. The commits are not erased immediately, so recovery works until Git garbage-collects them.

Explain it simply

Deleting a branch deletes only the label, not the commits. Think of removing a bookmark: the web page is still there until the server cleans it up.

Git still holds those commits, and the reflog remembers the last place HEAD saw them. Look for the line mentioning your branch or its last commit message, copy the hash, and create a new branch there. When you use git branch -d, Git also prints the abbreviated hash when it deletes, so scroll up in your terminal. On GitHub a deleted remote branch from a pull request has a Restore button.

Example

bash
git branch -D feature/payment
# Deleted branch feature/payment (was 4e5f6a7).
git branch feature/payment 4e5f6a7
# or search:
git reflog | grep payment

What to say in the interview

"Deleting a branch only removes the pointer. I use git reflog, or the hash printed at deletion, and run git branch name hash to recreate it. It works until unreachable commits are garbage collected."

#git#reflog#branch

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.