在 Git 中恢复已删除的分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16793637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:15:47  来源:igfitidea点击:

Recover deleted branch in Git

gitgit-branch

提问by Nalini Wanjale

I have deleted my branch by mistake like this:

我错误地删除了我的分支,如下所示:

git branch -D demo

But I want to recover it… I get this after git reflog

但我想恢复它......我得到了这个 git reflog

541b2f5 HEAD@{23}: checkout: moving from demo to master
06fa6d5 HEAD@{24}: commit (merge): remove ajax call for deleting variables and transfomers
b84b60a HEAD@{25}: checkout: moving from demo1 to demo

I want to create branch with sha 06fa6d5… so I tried this:

我想用 sha 创建分支06fa6d5……所以我尝试了这个:

git checkout -b demo  06fa6d5

git checkout -b demo  HEAD@{24}

But I didn't get code from that…

但我没有从中得到代码......

回答by vitthal

Create a list of all dangling or unreachable commits.

创建所有悬空或无法访问的提交的列表。

git fsck --full --no-reflogs --unreachable --lost-found

Print a list of commit messages for all commits in the lost and found.

打印丢失和找到的所有提交的提交消息列表。

ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline

Find your missing commit through the process of manual inspection (i.e. reading). Create a new branch with the missing commit as the branch head.

通过手动检查(即阅读)的过程找到您丢失的提交。以缺少的提交作为分支头创建一个新分支。

git checkout -b branch-name SHA

回答by Philip Oakley

Having got the potential sha1 for the last tip of branch demo, use gitk sha1to actually browse the commit's history to check you have the right one.

获得了 branch 的最后一个提示的潜在 sha1 demo,用于gitk sha1实际浏览提交的历史记录以检查您是否拥有正确的历史记录。