0

I deleted almost all branches on our team remote repo (github) by running git push -f --mirror.

I intended to publish a renamed branch like proposed in a comment on this StackOverflow Question. I didn't read see part:

but you should only use this method if the remote is simply to be a copy of your current repository.

The git output looked like this:

Enumerating objects: 347, done.
Counting objects: 100% (347/347), done.
Delta compression using up to 12 threads
Compressing objects: 100% (323/323), done.
Writing objects: 100% (329/329), 41.10 KiB | 2.28 MiB/s, done.
Total 329 (delta 223), reused 0 (delta 0)
remote: Resolving deltas: 100% (223/223), completed with 14 local objects.
remote: fatal: Invalid revision range 9777ec4fd3478671a130a5977ce1ac5946563795..0000000000000000000000000000000000000000
remote: fatal: Invalid revision range 3d3ee67027e8567e83c279cecba8dcd043414d1b..0000000000000000000000000000000000000000
remote: fatal: Invalid revision range 580282c713107ee5e20ca339634cae3ef623dc4f..0000000000000000000000000000000000000000
[...]
To https://github.com/.../.git
 - [deleted]           A
 - [deleted]           B
 - [deleted]           C
[...]
 - [new branch]        localBranch -> localBranch
 - [new branch]        origin/A -> origin/A
 - [new branch]        origin/B -> origin/B
 - [new branch]        origin/C -> origin/C
[...]

Is there a way to restore the branches? And does [new branch] origin/C -> origin/C mean, that this branch is now somewhere (where I can't find it)?

I'm really thankful for your help!

Kaniee
  • 111
  • 1
  • 10
  • 1
    I think the number one tip I can give anyone new to git is that **you should never do `git push -f`** unless you understand *fully* what happens. Never. Ever. Now, in order to fix your problem you will have to 1) track down all the branches you deleted, they may exist in other clones, and 2) repush them to github, or you will have to manually re-create each branch on the github repository, hoping that github hasn't done garbage collection yet. – Lasse V. Karlsen Jan 13 '21 at 14:53
  • 1
    To recreate a branch, first find the commit it referenced, you can do this using a direct url, like this: `/tree/`, and then in the dropdown that now contains the hash, to the left above the file list, click the button for the drop down and then type in the new name of a branch and click "Create branch ". As an example, here's a direct link to a commit in one of my repos: `https://github.com/lassevk/LVK/tree/54068df` – Lasse V. Karlsen Jan 13 '21 at 14:55
  • In order to rename a branch, in the future, the steps are to first create the new branch name and push that, then to simply delete the old. Again, as a golden rule, **never ever use `git push -f`** – Lasse V. Karlsen Jan 13 '21 at 14:58
  • 2
    I'd suggest immediately escalating to whoever manages your relationship with GitHub, so they can reach out to GH support. – jonrsharpe Jan 13 '21 at 14:59
  • Those "origin" branches probably needs to be deleted on github, as they are not properly referencing anything remote from githubs perspective. This is one of the issues with *mirror*, it isn't meant to publish a repository, it's meant for moving a repository from one server to another. – Lasse V. Karlsen Jan 13 '21 at 14:59
  • @jonrsharpe Agreed, that should've been my first tip as well. One of the first things one should do when messing up things with `git push -f` is to simply step away from the keyboard because clearly you don't know what you're doing, so *doing more things* is probably not the right thing anyway. – Lasse V. Karlsen Jan 13 '21 at 15:00
  • @LasseV.Karlsen Using the URL seams to to the trick. I'm going now through all the 50 branches to restore them. – Kaniee Jan 13 '21 at 15:09

0 Answers0