简体   繁体   English

恢复删除的git分支你不知道名字

[英]restore deleted git branch you don't know the name of

So a colleague has asked me (as team trainee GitHub manager) if it's possible to recover a branch he created months ago, and recently deleted but can't remember the name of. 所以一位同事问我(作为团队实习生GitHub经理)是否有可能恢复他几个月前创建的分支,并且最近删除但不记得名字。 Our working tree is as follows: 我们的工作树如下:

  1. master branch (Main production version) 主分公司(主要生产版)
  2. Staging branch (Testing before release to master) 暂存分支(在发布之前测试到master)
  3. Development branch (Main development branch where all future branches are created from 开发分支(主要开发分支,所有未来的分支都是从中创建的
  4. Personal branches (our induvidual branches that we make changes on, created from Development and then merged back into Development when complete) 个人分支机构(我们的变更分支,由开发创建,然后在完成后合并回开发)

The branch in question was created from Development, but it was never merged back in as it got old and work priorities pushed it back. 有问题的分支是从开发中创建的,但它从来没有合并过,因为它已经老了,工作优先级推迟了它。 Now there's time to work on it again we were wondering if it's possible to retrieve some kind of history that we can use to help us remember the branch name so we can then recover it...if at all possible... 现在有时间再次研究它我们想知道是否有可能找回我们可以用来帮助我们记住分支名称的某种历史记录,以便我们可以恢复它......如果可能的话......

Any advice would be appreciated! 任何意见,将不胜感激! Have googled but most answers are based on branches that have been merged back in. 谷歌搜索,但大多数答案是基于已合并的分支。

You can use git reflog to check where the HEAD was point. 您可以使用git reflog来检查HEAD的位置。

There may has the information like checkout: moving from develop to personal_branch , and the commit was the personal_branch where HEAD point to. 可能有像checkout: moving from develop to personal_branch这样的信息checkout: moving from develop to personal_branch ,提交是HEAD指向的personal_branch。

If the log history is cleaned in .git/logs unfortunately, you can use git fsck --full instead. 如果遗憾的是在.git/logs清除了日志历史.git/logs ,则可以改用git fsck --full

Per Marina - MSFT's answer, best case would be to find help in the reflog of the repo that originally created the branch (since it has the best chance of having relevant reflog entries). Per Marina - MSFT的答案,最好的情况是在最初创建分支的repo的reflog中找到帮助(因为它最有可能获得相关的reflog条目)。

But if that doesn't help, plan B would be to locate the dangling commits. 但如果这没有帮助,计划B将找到悬挂的提交。 Again this has the best chance of success in the repo that once housed the branch assuming it was deleted recently. 假设它最近被删除,那么这个曾经在分支机构中成功的回购中最有可能获得成功。 (Once the commits became unreachable, it becomes possible that gc will remove the commits entirely, and then the jig is up.) One way to look for them would be (一旦提交变得无法访问, gc就有可能完全删除提交,然后夹具就会启动。)查找它们的一种方法是

git fsck --unreachable --verbose

and check the output for something like 并检查输出的类似内容

unreachable commit c0d3abcd...

You'll probably see several if you see any; 如果你看到任何东西,你可能会看到几个; you can use 您可以使用

git cat-file -p c0d3abcd

to see what each one is. 看看每个人是什么。 If you find one that represents the state you want to work from, put a branch on it. 如果您找到一个代表您想要工作的状态,请在其上放置一个分支。 (The original branch name will basically be unrecoverable at this point.) (此时原始分支名称基本上是不可恢复的。)

If the local repo has lost the objects to a gc , you could try the remote. 如果本地存储库已将对象丢失为gc ,则可以尝试使用该遥控器。 It probably won't send the unreachable objects on a clone or pull request (though it's possible that it will depending on server implementation, you can't count on it), but it may provide API's for checking its reflogs or unreachable objects. 它可能不会在克隆或拉取请求上发送无法访问的对象(尽管它可能取决于服务器实现,但您不能指望它),但它可能提供用于检查其reflog或无法访问的对象的API。

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

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