-1

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?

Tawseef Bhat
  • 370
  • 4
  • 12

4 Answers4

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

I assume the developer still has his local repo, right? The terminal 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.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • branch reflog is deleted when branch is deleted, so this would have to be done with the HEAD reflog - which is not very easy, and won't work at all unless the coworker happened to have the latest commit of the branch checked out at some recent time. (I just verified this on 2.10.1 and it was true for any older version; if it's been changed in recent versions, great, but I don't think so.) The terminal was not necessarily on the branch tip commit at the time the branch was deleted - in fact, I doubt it. – Mark Adelsberger May 10 '19 at 17:36
  • that makes no sense. the branch can be deleted and reflog won't change because HEAD had already been there (if that is the case). – eftshift0 May 10 '19 at 17:37
0

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

kan
  • 28,279
  • 7
  • 71
  • 101
0

Below command will show the list of commits

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

Choose the commit from the deleted branch and checkout

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256