📖 Scenario: You accidentally deleted a branch in your Git repository but realize you need to recover it. Git keeps a record of recent changes, so you can find the deleted branch's commit and restore it.
🎯 Goal: Learn how to find the commit hash of a deleted branch and recreate the branch to recover lost work.
📋 What You'll Learn
Use git reflog to find the commit hash of the deleted branch
Create a new branch pointing to the recovered commit hash
Verify the branch is restored by listing all branches
💡 Why This Matters
🌍 Real World
Accidentally deleting a branch is common. Knowing how to recover it quickly saves time and prevents data loss.
💼 Career
Git branch recovery is a valuable skill for developers and DevOps engineers to maintain code integrity and workflow continuity.
Progress0 / 4 steps
1
Find the commit hash of the deleted branch
Run git reflog to see the recent commits and find the commit hash of the deleted branch.
Git
Hint
The git reflog command shows a list of recent commits and actions, including deleted branches.
2
Create a new branch at the recovered commit hash
Use git branch recovered-branch <commit-hash> to create a new branch called recovered-branch at the commit hash you found in Step 1. Replace <commit-hash> with the actual hash.
Git
Hint
Replace abc1234 with the actual commit hash from git reflog.
3
Switch to the recovered branch
Run git checkout recovered-branch to switch to the recovered branch.
Git
Hint
Use git checkout to move to the branch you just created.
4
Verify the recovered branch exists
Run git branch to list all branches and confirm recovered-branch is present.
Git
Hint
The git branch command shows all branches. Look for recovered-branch in the list.
Practice
(1/5)
1. What is the primary Git command used to find the commit hash of a recently deleted branch?
easy
A. git reflog
B. git branch -d
C. git reset --hard
D. git merge
Solution
Step 1: Understand the purpose of git reflog
git reflog records all changes to the HEAD, including deleted branches.
Step 2: Identify the command that shows commit history including deleted refs
git reflog lets you find the commit hash of the deleted branch.
Final Answer:
git reflog -> Option A
Quick Check:
Find deleted branch commit = git reflog [OK]
Hint: Use git reflog to find lost commits fast [OK]
Common Mistakes:
Confusing git branch -d as recovery command
Using git reset --hard which discards changes
Thinking git merge recovers deleted branches
2. Which of the following commands correctly recreates a deleted branch named feature1 from a commit hash abc1234 and switches to it?
easy
A. git checkout -b feature1 abc1234
B. git reset feature1 abc1234
C. git branch feature1 abc1234
D. git checkout abc1234 feature1
Solution
Step 1: Understand the syntax to create a branch at a commit
The correct syntax is git checkout -b [branch] [commit] to create and switch.
Hint: Use reflog commit hash with git checkout -b [OK]
Common Mistakes:
Using git branch -d which deletes branches
Using git reset --hard which resets HEAD but not branches
Trying git checkout bugfix without recreating branch
4. You tried to recover a deleted branch using git checkout -b feature abc123 but got an error: fatal: A branch named 'feature' already exists. What is the best way to fix this?
medium
A. Use git checkout feature to switch to the existing branch
B. Delete the existing branch first with git branch -D feature then retry
C. Use git branch -b feature abc123 instead
D. Rename the commit hash to a new one
Solution
Step 1: Understand the error
The error means branch feature already exists locally.
Step 2: Delete existing branch to recreate
Use git branch -D feature to force delete the existing branch, then recreate it.
Final Answer:
Delete existing branch with git branch -D feature then retry -> Option B
Quick Check:
Delete conflicting branch before recreate [OK]
Hint: Delete existing branch with -D before recreating [OK]
Common Mistakes:
Trying git branch -b which is invalid syntax
Ignoring the existing branch and forcing checkout
Renaming commit hash which is not possible
5. You accidentally deleted a branch release and want to recover it. You run git reflog and find multiple commits related to release. Which sequence of commands correctly recovers the branch at the latest commit and deletes the old branch pointer?
hard
A. git checkout -b release
B. git checkout release; git reset --hard
C. git branch -D release; git checkout -b release
D. git branch release
Solution
Step 1: Delete any existing branch pointer named release
Use git branch -D release to remove old or conflicting branch references.
Step 2: Create a new branch release at the desired commit
Use git checkout -b release <commit-hash> to recreate and switch to the branch at the correct commit.