Bird
Raised Fist0
Gitdevops~10 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the purpose of git reflog

    git reflog records all changes to the HEAD, including deleted branches.
  2. Step 2: Identify the command that shows commit history including deleted refs

    git reflog lets you find the commit hash of the deleted branch.
  3. Final Answer:

    git reflog -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify the correct command

    git checkout -b feature1 abc1234 creates branch feature1 at commit abc1234.
  3. Final Answer:

    git checkout -b feature1 abc1234 -> Option A
  4. Quick Check:

    Create branch at commit = git checkout -b branch commit [OK]
Hint: Use git checkout -b branch commit to restore branch [OK]
Common Mistakes:
  • Using git checkout commit branch without -b
  • Using git reset which does not create branches
  • Using git branch without switching
3. After deleting a branch bugfix, you run git reflog and see this entry:
abc1234 HEAD@{2}: checkout: moving from bugfix to main

Which command will correctly restore the bugfix branch at that commit?
medium
A. git checkout bugfix
B. git branch -d bugfix abc1234
C. git reset --hard abc1234
D. git checkout -b bugfix abc1234

Solution

  1. Step 1: Identify commit hash from reflog

    The commit hash abc1234 is where bugfix was last checked out.
  2. Step 2: Use command to recreate branch at that commit

    git checkout -b bugfix abc1234 creates and switches to bugfix at that commit.
  3. Final Answer:

    git checkout -b bugfix abc1234 -> Option D
  4. Quick Check:

    Restore branch from commit = git checkout -b branch commit [OK]
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

  1. Step 1: Understand the error

    The error means branch feature already exists locally.
  2. Step 2: Delete existing branch to recreate

    Use git branch -D feature to force delete the existing branch, then recreate it.
  3. Final Answer:

    Delete existing branch with git branch -D feature then retry -> Option B
  4. 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

  1. Step 1: Delete any existing branch pointer named release

    Use git branch -D release to remove old or conflicting branch references.
  2. 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.
  3. Final Answer:

    git branch -D release; git checkout -b release <commit-hash> -> Option C
  4. Quick Check:

    Delete old branch then recreate at commit [OK]
Hint: Delete old branch first, then recreate at commit [OK]
Common Mistakes:
  • Using git checkout release when branch is deleted
  • Using git reset --hard without recreating branch
  • Using git branch release without switching
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard