0
0
Gitdevops~10 mins

Recovering deleted branches in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Recovering deleted branches
Delete branch
Check reflog for branch commit
Find commit hash
Create new branch at commit
Branch recovered
This flow shows how to find the commit of a deleted branch using reflog and recreate the branch to recover it.
Execution Sample
Git
git branch -d feature

git reflog

git branch feature <commit-hash>
Delete a branch, find its last commit from reflog, then recreate the branch at that commit.
Process Table
StepCommandActionResult
1git branch -d featureDelete branch named 'feature'Branch 'feature' deleted
2git reflogShow recent HEAD changes including deleted branch commitsList of commits with hashes and messages
3Identify commit hash for 'feature' branchFind commit hash from reflog outputCommit hash abc123 identified
4git branch feature abc123Create new branch 'feature' at commit abc123Branch 'feature' recreated at abc123
5git branchList branches to confirm recoveryBranch 'feature' appears in list
💡 Branch 'feature' is recovered by recreating it at the last commit found in reflog.
Status Tracker
VariableStartAfter Step 1After Step 4Final
Branches['main', 'feature']['main']['main', 'feature']['main', 'feature']
Commit hashN/AN/Aabc123abc123
Key Moments - 2 Insights
Why do we use 'git reflog' to recover a deleted branch?
Because 'git reflog' records all recent commits and HEAD movements, including those from deleted branches, so we can find the commit hash to recreate the branch (see execution_table step 2 and 3).
What happens if you try to recreate the branch with a wrong commit hash?
The branch will point to the wrong commit, so your recovered branch won't have the expected changes. It's important to pick the correct commit hash from reflog (see execution_table step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what command is used to find the commit hash of the deleted branch?
Agit branch -d feature
Bgit reflog
Cgit branch feature <commit-hash>
Dgit checkout feature
💡 Hint
Check step 2 in the execution table where the command lists recent commits.
At which step is the branch actually recreated?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where 'git branch feature abc123' is run.
If the commit hash was not found in reflog, what would happen?
ABranch cannot be recovered using this method
BBranch can still be recreated at HEAD
CGit automatically recovers the branch
DBranch is deleted permanently but reflog shows it
💡 Hint
Refer to the concept flow and execution table steps 2 and 3 about finding the commit hash.
Concept Snapshot
Recovering deleted branches:
1. Delete branch: git branch -d <name>
2. Find last commit with: git reflog
3. Identify commit hash of deleted branch
4. Recreate branch: git branch <name> <commit-hash>
5. Confirm branch recovery with git branch
Full Transcript
To recover a deleted branch in git, first delete the branch using 'git branch -d branchname'. Then use 'git reflog' to see recent commits and find the commit hash where the branch pointed before deletion. Once you have the commit hash, recreate the branch with 'git branch branchname commit-hash'. Finally, check with 'git branch' to confirm the branch is back. This process works because reflog keeps a history of HEAD movements including deleted branches.