git github 是否会在历史记录中保留已删除的远程分支?如果是这样,那些可以恢复吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4674226/
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
Does github keep deleted remote branches in history? If so, can those be restored?
提问by luisgo
I was wondering if there is a way to restore a remote deleted branch in github. History clearly keeps record of the branch and merges with other branches but I'm not sure if it's possible to restore a deleted branch.
我想知道是否有办法在 github 中恢复远程删除的分支。历史记录清楚地保留了分支的记录并与其他分支合并,但我不确定是否可以恢复已删除的分支。
Thanks.
谢谢。
回答by Highway of Life
Yes, it's possible to restore a deleted branch from git.
是的,可以从 git 恢复已删除的分支。
Find your Commit ID: Search for a branch using git reflog
查找您的提交 ID:使用 git reflog 搜索分支
If you had the branch in your local git repo within the last 30 days, you may be able to find it in the reflogusing the following:
如果您在过去 30 天内在本地 git 存储库中有该分支,则可以使用以下命令在 reflog 中找到它:
git reflog
Search for the branch name in the reflog and note the HEAD{x}
point or the commit ID.
在 reflog 中搜索分支名称并记下该HEAD{x}
点或提交 ID。
Re-create the branch from the Reflog HEAD point:
从 Reflog HEAD 点重新创建分支:
git checkout -b branch_name HEAD@{27}
Re-create the branch from the commit ID:
从提交 ID 重新创建分支:
You can checkout the commit ID and create a branch off of that commit point:
您可以检出提交 ID 并从该提交点创建一个分支:
git checkout -b branch_name <commit id>
回答by VonC
It is possible to ask for GitHub support and have them look into the reflog
of your remote repo (like in this thread for example).
If this is close enough (less than 30 days per default) from the deletion, the reflog still contains the commits which are no longer referenced by any branch.
Creating a branch on one of those commits allow them to be again accessible.
可以请求 GitHub 支持并让他们查看reflog
您的远程存储库(例如在此线程中)。
如果距离删除足够接近(默认情况下少于 30 天),则引用日志仍包含不再被任何分支引用的提交。
在这些提交之一上创建一个分支允许它们再次被访问。
For more on reflog, see "what the heck is a reflog and why is it so important?"
有关 reflog 的更多信息,请参阅“什么是 reflog,为什么它如此重要?”
Update: the repo owner can also query the GitHub EVents API:
See "Does GitHub remember commit IDs?"
更新:repo 所有者还可以查询 GitHub EVents API:
请参阅“ GitHub 是否记得提交 ID?”
回答by Gordon Bean
When the branch has been deleted for a very long time (in my case, 1 year), but you had opened a pull request for that branch, you may be able to resurrect it by searching in the pull requests history.
如果分支已被删除很长时间(在我的情况下为 1 年),但您已经为该分支打开了拉取请求,您可以通过在拉取请求历史记录中搜索来恢复它。
Once I found the pull request for that branch I could restore the branch. Relevant commit information, etc. are also available from the pull request.
一旦我找到该分支的拉取请求,我就可以恢复该分支。相关的提交信息等也可以从拉取请求中获得。
回答by Alexander Bird
git reflog
will show you the history of HEAD
. If the branch you deleted was named foo
, then in that output, you should see lines like 48534f5 HEAD@{0}: checkout: moving from master to foo
or 48534f5 HEAD@{1}: merge foo: Fast-forward
. You can search the output of git reflog
to figure out which commit must be the latest one that foo pointed to.
git reflog
将向您展示HEAD
. 如果您删除的分支名为foo
,则在该输出中,您应该看到类似48534f5 HEAD@{0}: checkout: moving from master to foo
或 的行48534f5 HEAD@{1}: merge foo: Fast-forward
。您可以搜索 的输出git reflog
以找出哪个提交必须是 foo 指向的最新提交。
Do realize, that the "foo" reflog file itself is deleted when foo was deleted, but since the HEAD's reflog is different it still exists.
请注意,当 foo 被删除时,“foo”引用日志文件本身也被删除,但由于 HEAD 的引用日志不同,它仍然存在。
回答by Jim Zucker
Take a look at this python script for github events. https://github.com/jimzucker/githubutils/blob/master/githubreflog.py
看看这个用于 github 事件的 python 脚本。 https://github.com/jimzucker/githubutils/blob/master/githubreflog.py
I created it to pull events and make them readable, you can pipe it in to grep and look for the branch you are interested in. if there is enough history you will see the delete event for the branch in question, the next line will be the last push event and that is the sha you are interested in.
我创建它是为了拉取事件并使它们可读,您可以将其输入 grep 并查找您感兴趣的分支。如果有足够的历史记录,您将看到相关分支的删除事件,下一行将是最后一个推送事件,这就是您感兴趣的 sha。