0
0
Gitdevops~20 mins

Recovering deleted branches in Git - Practice Problems & Coding Challenges

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