简体   繁体   English

在'git push --mirror'之后恢复已删除的分支

[英]Recover deleted branches after 'git push --mirror'

My organization has a remote repository on GitHub that's a fork off of another. 我的组织在GitHub上有一个远程存储库,它是另一个存储库的分支。 I've made a clone on my local machine as well as created a local branch, 'test'. 我在我的本地机器上制作了一个克隆,并创建了一个本地分支'test'。 The following is a list of commands that followed: 以下是以下命令列表:

git fetch origin '+refs/heads/*:refs/remotes/origin/*'
git push --mirror
git push --all

What followed next was nothing short of a nightmare. 接下来的事情就是噩梦。 Any branch on the remote that wasn't checked out on my local machine was deleted on the repository. 在我的本地计算机上未检出的任何远程分支已在存储库中删除。 I've done git branch -a but that only shows, for the remote, branches that had existed on my local copy. 我已经完成了git branch -a但是只显示了我本地副本上存在的远程分支。 I've done git reflog show but that only shows logs on the current branch HEAD . 我已经完成了git reflog show但只显示当前分支HEAD上的日志。 Doing git fsck shows 6 'dangling commits' and 2 'dangling blobs'. git fsck显示6'悬空提交'和2'悬空blob'。 Prior to doing git push --mirror the branch had 16 branches, 5 of which were created by our organization and the rest were from the original fork. 在执行git push --mirror之前,分支有16个分支,其中5个由我们的组织创建,其余分支来自原始分支。 I understand that similar, if not the same, questions have been asked before but at this point none of them are making sense. 我知道之前已经提出了类似的问题(如果不是相同的话),但在这一点上,这些问题都没有任何意义。 My utmost respect and highest gratitude to anyone who can prevent me from getting fired. 我对能阻止我被解雇的任何人表示最崇高的敬意和最高的谢意。

There's a few things to try. 有几件事要尝试。 These first two approaches will help you recover the SHA1s of things that used to be branch tips. 前两种方法将帮助您恢复以前是分支提示的SHA1。 But one thing that will be hard to recover is the names of the missing branches, unless that was previously known. 但有一点难以恢复的是失踪分支的名称 ,除非之前已知过。

  1. If some of the branches were recently visited by you, your reflog will have their tips: 如果您最近访问过某些分支机构,您的reflog将提供以下建议:

     git reflog 
  2. You can create a list of all the dangling and lost-and-found commits with: 您可以使用以下命令创建所有悬空和丢失和找到的提交的列表:

     git fsck --full --no-reflogs --unreachable --lost-found | \\ grep commit | \\ cut -d" " -f3 | \\ xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt 

    and then see if the commits match the missing branches. 然后查看提交是否与丢失的分支匹配。

If you know the name of a branch you want to recover, then you can also try: 如果您知道要恢复的分支的名称,那么您还可以尝试:

  1. You can use git-resurrect.sh in the contrib directory to attempt to "resurrect" past branches by scanning your reflog and other sources. 您可以在contrib目录中使用git-resurrect.sh来尝试通过扫描您的reflog和其他来源来“复活”过去的分支。 This is extremely slow and can take a long time for large repositories. 这非常慢,大型存储库可能需要很长时间。

If you have filesystem access to the remote that you pushed to, you can recover the deleted branches from MYREPO.git/info/refs . 如果您对推送到的遥控器有文件系统访问权限,则可以从MYREPO.git/info/refs恢复已删除的分支。

The contents looks something like this. 内容看起来像这样。

2ef208a632946dd4b5fd6f124a7fd6fc1c2ab87e        refs/heads/foo-feature
7325d39f52d6663e33f360b3bd666300ce2c52b0        refs/heads/bar-feature

For each branch you want to recover, create a file in your local repo. 对于要恢复的每个分支,请在本地仓库中创建一个文件。

$ echo 2ef208a632946dd4b5fd6f124a7fd6fc1c2ab87e > .git/refs/heads/foo-feature

Then, push your newly-recovered branches back to the server. 然后,将新恢复的分支推送回服务器。

$ git push --all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM