# Restore a deleted branch with git reflog

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/restore-deleted-branch-full-en
A deleted branch can be recreated at a commit that is still reachable, and the reflog is the usual place to find that commit.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-09-A, TE-09-B and TE-09-C. The expected non-zero exit is TE-09-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-09-A: git branch -d refuses to delete an unmerged branch, and the commit stays addressable

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
git checkout -q -b feat; printf 'feat\n' > g.txt; git add g.txt; git commit -qm feat; git rev-parse HEAD > ../feat.sha
git checkout -q main
```

Paths written as `../name` sit one level above the repository folder, inside the same throwaway tree, so they do not appear in `git status`.

Command:

```bash
git branch -d feat 2>../del.err; echo delete=$?
```

Recorded output:

```
delete=1
```

Check command and its recorded output:

```bash
grep -q 'not fully merged' ../del.err && echo msg=ok; FEAT=$(cat ../feat.sha); git branch restored $FEAT; echo same=$([ "$(git rev-parse restored)" = "$FEAT" ] && echo yes); git branch -D feat >/dev/null 2>&1; echo type=$(git cat-file -t $FEAT)
```

```
msg=ok
same=yes
type=commit
```

### TE-09-B: a branch deleted with -D can be recreated from the reflog entry of its last commit

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
git checkout -q -b feat; printf 'feat\n' > g.txt; git add g.txt; git commit -qm feat
git checkout -q main; git branch -D feat >/dev/null; echo deleted=$?; git reflog | grep -c feat
```

Command:

```bash
SHA=$(git reflog | grep -m1 'commit: feat' | cut -d' ' -f1); git branch feat $SHA; echo restored=$?; echo subject=$(git log -1 --format=%s feat); echo count=$(git rev-list --count feat)
```

Recorded output:

```
restored=0
subject=feat
count=2
```

### TE-09-C: expected non-zero exit: after git branch -D the branch name is gone, and checking it out fails

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
git checkout -q -b gone; printf 'g\n' > g.txt; git add g.txt; git commit -qm gone
git checkout -q main; git branch -D gone >/dev/null
```

Command:

```bash
git checkout gone
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
error: pathspec 'gone' did not match any file(s) known to git
```

Exit status: 1. This status and the output match what was written before the run.

TE-09-C: by the setup, the branch gone was deleted with -D. git checkout gone then exited with status 1 and printed error: pathspec 'gone' did not match any file(s) known to git.

## Limits

Deleting the branch deletes its reflog; once reflog entries expire or objects are pruned, the commit may no longer be recoverable.

## Source

- https://git-scm.com/docs/git-reflog
- https://git-scm.com/docs/git-gc

## Evidence

- `TE-09-A`: git branch -d refuses to delete an unmerged branch, and the commit stays addressable
- `TE-09-B`: a branch deleted with -D can be recreated from the reflog entry of its last commit

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-09-A` | git branch -d refuses to delete an unmerged branch, and the commit stays addressable | 0 | PASS | `9c8b1ae6ef51` |
| `TE-09-B` | a branch deleted with -D can be recreated from the reflog entry of its last commit | 0 | PASS | `5994ccd8b74d` |
| `TE-09-C` | expected non-zero exit: after git branch -D the branch name is gone, and checking it out fails | 1 | PASS | `b85a62e037fe` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Git Merge vs Rebase: How Each Integrates Changes and Shapes History](https://kairos-2z6s.onrender.com/res/merge-vs-rebase-full-en)
- [git bisect: find the commit that broke something](https://kairos-2z6s.onrender.com/res/git-bisect-full-en)
- [Git Switch: change branches](https://kairos-2z6s.onrender.com/res/git-switch-full-en)
- [Keeping API keys and Git secrets safe: a guide](https://kairos-2z6s.onrender.com/res/secrets-security-hub-en)
- [Removing a committed secret from all Git history](https://kairos-2z6s.onrender.com/res/remove-secret-from-git-history-en)
- [Python Zip: loop over lists together](https://kairos-2z6s.onrender.com/res/python-zip-full-en)
