Bird
Raised Fist0
Gitdevops~20 mins

Recovering deleted branches in Git - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Branch Recovery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Recovering a recently deleted branch
You accidentally deleted a branch named feature-x. Which command will correctly restore it if the branch was deleted very recently and you know the branch's last commit is still in the reflog?
Agit checkout -b feature-x $(git reflog show feature-x -1 | awk '{print $1}')
Bgit checkout -b feature-x refs/reflog/feature-x
Cgit checkout -b feature-x $(git reflog | head -1 | awk '{print $1}')
Dgit checkout -b feature-x $(git reflog | grep feature-x | head -1 | awk '{print $1}')
Attempts:
2 left
💡 Hint
Use git reflog show feature-x to access the branch-specific reflog and extract the most recent commit hash.
🧠 Conceptual
intermediate
1:30remaining
Understanding reflog for branch recovery
What does the git reflog command track that makes it useful for recovering deleted branches?
AIt records all changes to the tip of branches and HEAD, including commits and checkouts.
BIt tracks all remote branches and their statuses.
CIt lists all files changed in the repository history.
DIt shows the current status of the working directory and staging area.
Attempts:
2 left
💡 Hint
Think about what information helps find lost commits or branches.
❓ Troubleshoot
advanced
2:30remaining
Recovering a deleted branch when reflog is cleared
You deleted a branch bugfix and also ran git reflog expire --expire=now --all which cleared the reflog. What is the most reliable way to recover the branch now?
ARun <code>git checkout -b bugfix origin/bugfix</code> to restore from remote tracking branch.
BRun <code>git branch bugfix</code> to recreate the branch with the same name.
CUse <code>git reset --hard HEAD@{1}</code> to undo the deletion.
DUse <code>git fsck --lost-found</code> to find dangling commits and create the branch from the correct commit hash.
Attempts:
2 left
💡 Hint
Think about how to find commits not referenced by any branch or reflog.
🔀 Workflow
advanced
3:00remaining
Steps to recover a deleted branch safely
Which sequence of commands correctly recovers a deleted branch release using reflog and then pushes it back to the remote repository?
A2,4,1,3
B2,1,4,3
C2,1,3,4
D1,2,4,3
Attempts:
2 left
💡 Hint
First find the commit, then create the branch, verify, and finally push.
✅ Best Practice
expert
2:00remaining
Preventing branch loss in collaborative projects
What is the best practice to avoid losing important branches accidentally in a shared Git repository?
AAvoid using branches and work only on the main branch to reduce complexity.
BAlways delete branches locally and never push them to remote.
CRegularly push branches to remote and protect important branches with branch protection rules.
DUse <code>git gc</code> frequently to clean up old branches automatically.
Attempts:
2 left
💡 Hint
Think about collaboration and safety features in Git hosting services.

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