CodeBlanks Logo Icon CodeBlanks

Disaster recovery

Find a deleted branch with reflog and recover it with reset.

Reflog and Disaster Recovery

In Git, an accidentally deleted branch or commits lost via git reset --hard do not disappear immediately. The git reflog command chronologically logs every movement of HEAD. Once the lost commit hash is identified in reflog, data is fully recovered using git reset --hard <hash> or by creating a new branch.

git reflog
# HEAD@{1}: commit: critical feature added
git branch restored-branch HEAD@{1}

Practice Recover Lost Commit with Reflog

A commit in the repository was undone with reset --hard HEAD~1. Run git reset --hard HEAD@{1} to restore the lost commit, and print the commit message to the terminal with git log -1 --pretty=%B.

Starter code

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "v1" > app.txt
git add app.txt
git commit -m "ilk commit" -q
echo "v2" > app.txt
git add app.txt
git commit -m "kurtarilacak ozellik" -q
git reset --hard HEAD~1 -q

# Reflog referansı kullanarak kayıp commit'e dön

# Son commit mesajını yazdır

Sample solution

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "v1" > app.txt
git add app.txt
git commit -m "ilk commit" -q
echo "v2" > app.txt
git add app.txt
git commit -m "kurtarilacak ozellik" -q
git reset --hard HEAD~1 -q

git reset --hard "HEAD@{1}" -q
git log -1 --pretty=%B

Expected output

kurtarilacak ozellik

Practice Recreate Deleted Branch with Reflog

The feature branch was deleted but its latest commit is at HEAD@{1}. Recreate the deleted branch with git branch feature HEAD@{1}. Then print the branch name to the terminal using git rev-parse --abbrev-ref feature.

Starter code

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "base" > base.txt
git add base.txt
git commit -m "base" -q
git checkout -b feature -q
echo "feature work" > feat.txt
git add feat.txt
git commit -m "feature commit" -q
git checkout master -q 2>/dev/null || git checkout main -q
git branch -D feature -q

# Silinen feature dalını reflog referansı HEAD@{1} ile yeniden oluştur

# Dal adını rev-parse ile yazdır

Sample solution

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "base" > base.txt
git add base.txt
git commit -m "base" -q
git checkout -b feature -q
echo "feature work" > feat.txt
git add feat.txt
git commit -m "feature commit" -q
git checkout master -q 2>/dev/null || git checkout main -q
git branch -D feature -q

git branch feature "HEAD@{1}"
git rev-parse --abbrev-ref feature

Expected output

feature

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the most accurate first step to identify and recover the latest commit of an accidentally deleted branch?

  • Running git reflog to find the HEAD position before the branch was deleted Correct answer
  • Scanning all files one by one with git fsck --lost-found
  • Reversing the last change using git revert HEAD
  • Manually opening all files in the .git/objects directory

Explanation: Because git reflog retains every movement of the HEAD reference, it immediately shows the last commit hash of the deleted branch, facilitating recovery.

2 Branch Recovery via Reflog Reference

Question
git branch restore-feature ____
Answer
git branch restore-feature HEAD@{1}

Explanation: The HEAD@{1} reflog pointer references HEAD's exact previous position, re-creating the deleted branch at that commit.

3 Recovery Terms

Term Meaning
git reflog Lists HEAD movement history
HEAD@{0} Current position of HEAD
git reset --hard Forcefully syncs working dir
Dangling commit Commit without any references

All lessons in Expert