One of your teammates accidentally deleted a branch, and has already pushed the changes to the central git repo. There are no other git repos, and none of your other teammates had a local copy. How would you recover this branch?

4 Answers

0
Community On
$ git reflog
1ed7510 [email protected]{1}: checkout: moving from develop to 1ed7510
3970d09 [email protected]{2}: checkout: moving from b-fix-build to develop
1ed7510 [email protected]{3}: commit: got everything working the way I want
70b3696 [email protected]{4}: commit: upgrade rails, do some refactoring
98f2fc2 [email protected]{5}: commit: a couple code cleanups
d09f35e [email protected]{6}: commit: remove test method - it served it's purpose and now it must go
d586a93 [email protected]{10}: commit: aha! that is why I'm so fail
4644046 [email protected]{11}: commit: cleaning up the initial migration for dev/test environments
323df37 [email protected]{15}: commit: bump ruby version
eab861c [email protected]{16}: commit: bundle update EVERYTHING
2b544c4 [email protected]{17}: commit: fixing what few tests actually exist - a.k.a., wow! does this app even work?
3970d09 [email protected]{18}: checkout: moving from develop to b-fix-build
3970d09 [email protected]{19}: pull: Fast-forward

Once you find the commit you're looking for, create a new branch from it and you're done!

$ git checkout -b branch-name 70b3696
Switched to a new branch 'branch-name'
0
eftshift0 On

I assume the developer still has his local repo, right? The terinal should have the id of the revision when he pushed the removal... use that ID to create a new branch

git branch blah the-id

If the terminal is already closed, then check git reflog for the last revision of that branch.

0
kan On

Easiest - look into "git reflog" of the teammate's local repo. You'll find last entry when he checked-out the branch last time.

If it's lost locally, you may have access to remote repo - look there for dangling commits. See instructions here: https://softwarebakery.com/recovering-commits-with-git

Just create a new branch on top of the found commit-id

0
Sazzad Hissain Khan On

Below command will show the list of commits

git fsck --full --no-reflogs | grep commit

Choose the commit from the deleted branch and checkout