Recover a Deleted Git Branch (Locally or on GitHub)
Find the commit the branch last pointed at, then create a branch ref at that commit, locally or on GitHub.
Goal
Restore a branch deleted with git branch -d, git branch -D, git push --delete, or the delete button on GitHub. The commits outlive the branch name, so recovery means finding the tip hash and pointing a new ref at it.
Why deletion is reversible
git branch -D removes the name under .git/refs/heads/. It does not delete the commits that name pointed at. The objects stay in the database until garbage collection prunes unreachable commits.
The branch's own log under .git/logs/refs/heads/ disappears with the name. Recovery depends on another record of the tip: the delete message, the HEAD reflog, a remote-tracking ref, or a pull request on GitHub.
Recover a local branch
Try these in order. Each step covers a less common case than the one before it.
1. Read the delete message. git branch -D prints the tip it removed:
Deleted branch feature/login (was d2ea1df).
If that line is still in your terminal, recreate the branch at that hash and stop:
git branch feature/login d2ea1df
2. Check for a remote-tracking ref. If the branch was pushed and you have not fetched with --prune since, origin/feature/login still points at the pushed tip:
git branch feature/login origin/feature/login
3. Search the HEAD reflog. It records every checkout that moved onto or off the branch:
git reflog
A line such as checkout: moving from feature/login to main marks the moment you left the branch. The hash on that line is the tip of feature/login at that point. Recreate the branch there, or check it out in one step:
git switch -c feature/login abc1234
4. Search unreachable commits with git fsck. If the branch was never checked out in this clone, the reflog has no record of it. Commits that no ref or reflog reaches are still in the object database until git gc prunes them:
git fsck --lost-found --no-reflogs
Git prints each dangling commit <sha> and writes those hashes to .git/lost-found/commit/. --no-reflogs also lists commits that only a reflog entry keeps alive. Inspect candidates with git log --oneline -5 <sha>, then run git branch <name> <sha> on the right one.
Confirm the result before you build on it:
git log --oneline -5 feature/login
Push when you need the branch on a remote again:
git push -u origin feature/login
Restore a deleted branch on GitHub
GitHub keeps the commits of every pull request after its head branch is deleted. That makes the pull request the most reliable record of a deleted remote branch. For finding deleted branches in the GitHub UI first, see Find and Restore Deleted Branches on GitHub.
Restore branch button. Open the repository's Pull requests tab, click Closed, and open the pull request for the branch. Near the bottom of the pull request, click Restore branch. GitHub recreates the branch at the pull request's last head commit. The button only covers the head branch of a closed or merged pull request, and it does not appear while an open pull request uses the branch. See GitHub's Deleting and restoring branches in a pull request.
Fetch the pull request head and push it. GitHub exposes every pull request's head commit as refs/pull/<number>/head, even after the branch is gone. Fetch it, then push that commit as a new branch:
git fetch origin pull/123/head:feature/login
git push -u origin feature/login
GitHub documents the fetch form in Checking out pull requests locally. If you already have the SHA locally, push it directly with git push origin <sha>:refs/heads/feature/login.
Use gh or the REST API. Read the head commit and branch name from the pull request, then create the ref with the Create a reference endpoint:
gh pr view 123 --json headRefName,headRefOid
gh api repos/{owner}/{repo}/git/refs \
-f ref=refs/heads/feature/login \
-f sha=<headRefOid>
The API needs the full 40-character SHA and rejects a ref that does not start with refs/.
Find a branch with no pull request. Open the repository's Activity view and filter the All activity menu to branch deletions. GitHub ties each deletion to a user and a commit. Open Compare changes from the entry's menu to see the commits and copy the branch's last commit hash. GitHub covers this view in Using the activity view. Push that SHA from any clone that has it, or create the ref through the API.
When recovery fails
These cases leave no record of the tip:
- The branch existed only on GitHub, had no pull request, and no clone ever fetched it.
- The reflog entry expired and
git gcpruned the unreachable objects. Unreachable reflog entries default to about 30 days undergc.reflogExpireUnreachable. - The commits lived only in a fork that its owner deleted, with no pull request into your repository.
Act before retention windows lapse. After a prune, the tip is gone from that clone, and only another clone or GitHub can supply it.
Related
- Find and Restore Deleted Branches on GitHub: the Activity view, the Restore branch button, and
gh api - Discarding Changes: Treq toast Undo for discarded working-copy edits
- What is Version Control?: commits, branches, and local recovery basics
- Merge vs Rebase: rewrite operations that move refs and leave tips to recover