How to Recover a Deleted Remote Branch on GitHub
I told Claude Code to “pull down those two unmerged branches” — meaning check them out locally — and it ran git push origin --delete instead. No local copy either. Here’s how to recover from that.
For Local Deletions (git reflog)
If you deleted a branch with git branch -d, you can restore it from git reflog.
# Check operation history
git reflog
# Example output: 4895a77 HEAD@{5}: checkout: moving from my-branch to main
# Use that commit hash
# Restore the branch
git checkout -b my-branch 4895a77
Reflog entries are kept for 90 days by default.
For Remote Deletions (GitHub API)
When a branch is deleted on the remote, reflog has no record. You can retrieve the repository’s activity log via the gh api command. Branch deletion events include the last commit hash before deletion.
gh api repos/{owner}/{repo}/activity \
--jq '.[] | select(.activity_type == "branch_deletion") | {ref: .ref, sha: .sha}'
Example output:
{
"ref": "refs/heads/feature/my-branch",
"sha": "d1525b761a6bdaed7df1058b68fd09da7b2fa81e"
}
The sha is the commit hash from just before deletion.
Restoring the Branch
Once you have the commit hash, create a new branch from it.
# Create branch locally
git checkout -b feature/my-branch d1525b761a6bdaed7df1058b68fd09da7b2fa81e
# Push to remote
git push -u origin feature/my-branch
Filtering for a Specific Branch
If you know part of the deleted branch name, filter with jq.
gh api repos/{owner}/{repo}/activity \
--jq '.[] | select(.ref | contains("my-branch")) | {ref: .ref, activity_type: .activity_type, sha: .sha}'
Both branch_creation and branch_deletion events will appear. Use the sha that isn’t 0000000... (that’s the deletion event).
Notes
- The retention period for activity logs is unclear — act quickly
- The commit itself remains in GitHub until garbage collection runs (typically a few months)
- Works with private repos as long as you’re authenticated via
gh
Prevention: Adding Guardrails to CLAUDE.md
After this incident, I added the following guardrail to CLAUDE.md.
### Branch Deletion Prohibited
Never execute branch deletion commands (`git branch -d`, `git branch -D`, `git push origin --delete`, etc.).
Instructions like "check out that branch" or "switch to that branch" can easily be misread as "delete." If there's any ambiguity, use AskUserQuestionTool to confirm.
Example: "Do you want to delete branch xxx, or check it out (switch to it)?"
I mean, misunderstandings happen — maybe I just need to be more explicit… but it worked fine before this!