Recovering an Accidentally Deleted Feature Branch
Scenario Prompt:
“A developer ran git branch -D feature-billing after believing it was merged into main. It was not merged, and the remote branch was also pruned. The branch held two days of commits that were never pushed. How do you recover the branch?”
Substantive Technical Concepts to Hit:
Inspect Local Reflog
Run git reflog to inspect recent HEAD movements and identify the SHA hash of the last commit made on the deleted branch.
Recreate Branch from Commit Hash
Execute git checkout -b feature-billing <commit-sha> to restore the branch pointing to the recovered commit.
Dangling Commit Verification
If the commit does not appear in reflog, use git fsck --lost-found to search the object database for unreferenced dangling commits.
Branch Protection Policies
Configure remote branch protection and deletion restrictions on production repositories to prevent accidental remote branch loss.
- Was the branch deleted locally on the developer workstation where commits were originally created?
- Has git gc --prune=now been executed since the deletion occurred?
- Do we need to back up the current .git directory before attempting object surgery?