Restore a deleted branch with git reflog
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.
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):
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 mainPaths written as ../name sit one level above the repository folder, inside the same throwaway tree, so they do not appear in git status.
Command:
git branch -d feat 2>../del.err; echo delete=$?Recorded output:
delete=1Check command and its recorded output:
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=commitTE-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):
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 featCommand:
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=2TE-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):
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/nullCommand:
git checkout goneRecorded output: none, standard output was empty.
Standard error from the same run:
error: pathspec 'gone' did not match any file(s) known to gitExit 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
Evidence
TE-09-A: git branch -d refuses to delete an unmerged branch, and the commit stays addressableTE-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.
All full guides: All guides