简体   繁体   English

恢复一周前删除的本地分支-Git

[英]Restore a local branch that was deleted week ago - Git

I deleted work on a local branch a month ago. 一个月前,我删除了本地分支机构的工作。 I don't remember what the branch name was called. 我不记得分支名称是什么。

Is there a way to show the names of all the local branches I've had The git reflog this doesnt seem to be of any use, it only shows about 20 commits and nothing about them being on a local branch or its name. 有没有一种方法可以显示我所拥有的所有本地分支的名称git reflog似乎没有用,它仅显示大约20个提交,而关于它们在本地分支或其名称上却什么也没有。

Is there a way to show the names of all the local branches 有没有办法显示所有本地分支的名称

To see the current local branches: 要查看当前的本地分支机构:

git branch 

if the local branch was not merged or deleted? 是否未合并或删除本地分支?

If you have not merged the local to any other branch of if you deleted it and git reflog is not helpful to you, you can always use the git fsck --lost-found to print out lists of dangling commits (commits which are not accessible from any other commit/branch). 如果您尚未将本地合并到其他分支(如果删除了该分支),并且git reflog对您没有帮助,则始终可以使用git fsck --lost-found打印出悬挂的提交列表(无法访问的提交)来自其他任何提交/分支)。

git fsck --lost-found

--lost-found

Write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/ , depending on type. 根据类型,将悬空对象写入.git/lost-found/commit/.git/lost-found/other/

If the object is a blob, the contents are written into the file, rather than its object name. 如果对象是Blob,则将内容(而不是对象名称)写入文件。

Once you have those commits you can simply print them out using: git show <SHA-1> and once you see a tree search for the root commit and you can now restore it. 一旦拥有了这些提交,您就可以使用以下命令将它们简单地打印出来: git show <SHA-1> ,一旦您看到树搜索根提交,现在就可以将其还原。


# find out all dangling (loos) objects 
git fsck --lost-found

# find out the desired root tree using the git cat-file or git show

# Search for tree objects
git cat-file -t <SHA-1>

#  once a tree was found print its content
git cat-file -p <SHA-1>

# OR

# again print it content in different way
git show <SHA-1>

# once you found your lost tree - recover it
git checkout <branch_name> <SHA-1>

Some more advanced options: 一些更高级的选项:

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

# --full       = Checkout all object in other locations 
                 (read doc to find all about it)

# --no-reflogs = This option is meant only to search for commits that used
                 to be in a ref, but now aren’t, but are still in that 
                 corresponding reflog.

# --unreachable= Print out objects that exist but that aren’t reachable from
                 any of the reference nodes.

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

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