shellstreak

Recover a deleted Git branch

Safe: nothing is lost

Short answer

Find the branch's last commit with git reflog (or in the Deleted branch … (was abc1234) message), then git branch <name> <commit>.

The commands, one by one

  1. 1git reflog

    Lists everywhere HEAD has been, newest first. Look for the last commit you made on the deleted branch: here it's HEAD@{1}, one move ago, before you switched back to main.

  2. 2git branch gift-cards HEAD@{1}

    Creates the branch again, pointing at that commit. All its commits are back.

For your own repository: If you still have the Deleted branch gift-cards (was 1a2b3c4) message, git branch gift-cards 1a2b3c4 works too. Reflog entries expire after about 90 days.

Try it safely

You deleted the unmerged gift-cards branch with git branch -D.

learner@shellstreak: ~
Starting your practice shell…

Type your own commands too. Reset puts the repository back to just after the mistake.

Practise this in 5 minutes

Lesson 74, Recover from an agent's mistakes, walks you through this undo and the ones around it in a safe terminal, step by step, and checks your answers.

Keep the terminal cheat sheet handy: free to print

Get the shellstreak newsletter

Short terminal and Git tips you can try in a minute, plus a heads-up when new free tools and lessons ship. It lands now and then, never a flood.

No spam, unsubscribe in one click.