-2

We are using Git and Stash at my firm and it looks like someone deleted all the branches but master.

Can someone please tell me how to track down which users did it? Also, how to restore the data (if it can be restored)?

SQB
  • 3,926
  • 2
  • 28
  • 49
JMS
  • 183
  • 2
  • 9

2 Answers2

1

The git fsck method in this question about how to do this for one branch might not be enough here, because with multiple deleted branches there might be some question which commits go with which branches. But if it turns out answering that is easy, it'll probably be fastest to do it that way:

git fsck --no-reflogs \
| awk '$2 == "commit" {print $3}' \
| xargs git show -s

If the Stash product doesn't keep sufficient logs, then if it does actually use git or something that implements git's reflogs under the hood you can use those to recover the deleted refs at least.

git removes the reflogs for explicitly-deleted branches, including pushed explicit deletions; those are now gone from your damaged repo and from the repo of whoever explicitly pushed the deletion.

But remotes doing plain fetches from your repo don't interpret that as a request to delete their branches tracking your refs -- fetch without an explicit request to delete isn't an explicit request to delete any more than push without an explicit request to delete is. For that, fetch has to be explicitly asked (with --prune) to prune branches no longer in the remote.

So repos that haven't done any explicit cleanup are pretty sure to still have remote-tracking branches for the missing ones, and you can look at the most recent transactions among their logs. Easiest way to automate that is

git for-each-ref refs/remotes/$remote --format='git reflog -1 %(refname)' \
# | sh -x

Branches in that output that don't have reflogs haven't been changed since the initial fetch.


Now you can go back to your damaged repo and if there's any question which of the commits the other repos remember for a branch is most recent, do

git log --graph --decorate --oneline $each $candidate $commit

to help you find it.

git branch $branch $therightcommit to restore it.


Maybe someone will know an easier way to do this with Stash (or git for that matter), if not, this might be worth a try.

Community
  • 1
  • 1
jthill
  • 55,082
  • 5
  • 77
  • 137
0

To recover: Use git reflog to find SHA1, then use git checkout <sha>.

To track down: Use git log to see all commmit logs - http://git-scm.com/docs/git-log

suuuzi
  • 602
  • 1
  • 8
  • 19