Recover a deleted branch

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Recreates a branch that was deleted locally by finding its last commit in the reflog and pointing a new branch at it.

gitbranchrecoverreflog

Parameters

Name to give the recovered branch.

Commit hash the deleted branch pointed to, found in the reflog.

Commands

Find the commit hash the deleted branch last pointed to

git reflog --no-abbrev

Recreate the branch at that commit

git branch <branch_name> <commit>

Verification

git log --oneline -1 <branch_name>

The recovered branch exists and its tip is the expected commit.

Undo

Delete the recreated branch again

git branch -D <branch_name>

Pitfalls

  • Reflog entries expire (90 days by default); very old deletions may be unrecoverable.
  • The reflog is local only; a branch deleted on another machine leaves no trace here.

Related