# 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](/learn/concepts/git/git-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:

```text
Deleted branch feature/login (was d2ea1df).
```

If that line is still in your terminal, recreate the branch at that hash and stop:

```bash
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:

```bash
git branch feature/login origin/feature/login
```

**3. Search the `HEAD` reflog.** It records every checkout that moved onto or off the branch:

```bash
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:

```bash
git switch -c feature/login abc1234
```

**4. Search unreachable commits with `git fsck`.** If the branch was never checked out in this [clone](/learn/concepts/git/git-worktrees-vs-clones), 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:

```bash
git fsck --lost-found --no-reflogs
```

Git prints each `dangling commit ` 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 `, then run `git branch  ` on the right one.

Confirm the result before you build on it:

```bash
git log --oneline -5 feature/login
```

[Push](/docs/how-to/pushing-to-remote) when you need the branch on a remote again:

```bash
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](/learn/answers/git/github-restore-deleted-branch).

**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](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-branches-in-your-repository/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//head`, even after the branch is gone. Fetch it, then push that commit as a new branch:

```bash
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](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally). If you already have the SHA locally, push it directly with `git push origin :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](https://docs.github.com/en/rest/git/refs#create-a-reference) endpoint:

```bash
gh pr view 123 --json headRefName,headRefOid
gh api repos/{owner}/{repo}/git/refs \
  -f ref=refs/heads/feature/login \
  -f sha=
```

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](https://docs.github.com/en/repositories/viewing-activity-and-data-for-your-repository/using-the-activity-view-to-see-changes-to-a-repository). 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 gc` pruned the unreachable objects. Unreachable reflog entries default to about 30 days under `gc.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](/learn/answers/git/github-restore-deleted-branch): the Activity view, the Restore branch button, and `gh api`
- [Discarding Changes](/docs/how-to/discarding-changes): Treq toast Undo for discarded working-copy edits
- [What is Version Control?](/learn/concepts/git/version-control): commits, branches, and local recovery basics
- [Merge vs Rebase](/learn/concepts/git/merge-vs-rebase): rewrite operations that move refs and leave tips to recover
