简体   繁体   English

浏览 Git 中的孤立提交

[英]Browse orphaned commits in Git

My git repository has somehow gone wonky - I loaded up msysgit this morning and instead of the branch name being shown after the current directory, it says "((ref: re...))", 'git status' reports everything as a new file, 'git log' and 'git reflog' tell me "fatal: bad default revision 'HEAD'", and so on.我的 git 存储库不知何故变得不稳定 - 我今天早上加载了 msysgit,而不是分支名称显示在当前目录之后,它说“((ref:re ...))”,“git status”将所有内容报告为新文件、'git log' 和 'git reflog' 告诉我“致命的:错误的默认修订版 'HEAD'”,等等。

Doing 'git reflog --all' or 'gitk --all' shows me the rest of the repository is intact, but it looks like the branch I was working on has just disappeared, which explains why HEAD doesn't seem to exist/point to anything.执行“git reflog --all”或“gitk --all”显示存储库的 rest 完好无损,但看起来我正在处理的分支刚刚消失,这解释了为什么 HEAD 似乎不存在/指向任何东西。

I know git keeps hold of all sorts of globs of information, and I'm assuming my commits have just been orphaned somehow, so is there some command that will show me those commits so I can reset HEAD to them?我知道 git 保存着各种各样的信息,我假设我的提交刚刚以某种方式被孤立,那么是否有一些命令会向我显示这些提交,以便我可以将 HEAD 重置为它们?

EDIT: Oh dear.编辑:哦,亲爱的。 I discovered 'git fsck', and 'git fsck --full' reports "fatal: object 03ca4... is corrupted".我发现了“git fsck”,而“git fsck --full”报告“fatal: object 03ca4... is corrupted”。 What the devil can I do about that?我到底能做什么呢?

EDIT: Oh dear oh dear.编辑:哦,天哪,天哪。 I checked out another branch, then tried to re-create the original branch with the same name using 'git checkout -b lostbranchname', and git says "error: unable to resolve reference refs/heads/lostbranchname: No error, fatal: Failed to lock ref for update: No error".我检查了另一个分支,然后尝试使用“git checkout -b lostbranchname”重新创建具有相同名称的原始分支,git 说“错误:无法解析引用 refs/heads/lostbranchname:没有错误,致命:失败锁定 ref 以进行更新:没有错误”。 'No error' must be a particularly nasty error. “没有错误”一定是一个特别讨厌的错误。 So it looks like it's still hanging around, but unable to be used and unable to be killed.所以看起来它仍然在徘徊,但无法使用并且无法被杀死。

EDIT: Super duper oh dear.编辑:超级骗子哦,亲爱的。 I've done a bunch of unpacking and repacking and replacing of things as suggested here: How to recover Git objects damaged by hard disk failure?我已经按照此处的建议进行了一系列拆包、重新打包和替换操作: How to recover Git objects damaged by hard disk failure? , but now I'm getting another hash reported as corrupt, for something as innocuous as 'git status'. ,但现在我收到另一个 hash 报告为损坏,因为一些像“git status”一样无害的东西。 I think the entire thing is hosed.我认为整个事情都被淹没了。 Git's lovely and all, but I shouldn't have to deal with this kind of thing. Git 很可爱,但我不应该处理这种事情。

Rather than leave this open I think I'll give an answer to my own question.我想我会回答我自己的问题,而不是让这个开放。 Using git reflog --all is a good way to browse orphaned commits - and using the SHA1 hashes from that you can reconstruct history.使用git reflog --all是浏览孤立提交的好方法 - 并使用其中的 SHA1 哈希值可以重建历史记录。

In my case though, the repository was corrupted so this didn't help;不过就我而言,存储库已损坏,因此这没有帮助; git fsck can help you find and sometimes fix errors in the repository itself. git fsck可以帮助您查找并有时修复存储库本身中的错误。

With git 2.9.x/2.10 (Q3 2016), you won't have to use git reflog --all anymore, git reflog will be enough.使用 git 2.9.x/2.10(2016 年第三季度),您将不再需要使用git reflog --allgit reflog就足够了。

See commit 71abeb7 (03 Jun 2016) by SZEDER Gábor ( szeder ) .请参阅SZEDER Gábor ( szeder ) 提交的 71abeb7 (2016 年 6 月 3 日)
(Merged by Junio C Hamano -- gitster -- in commit 7949837 , 06 Jul 2016) (由Junio C gitster合并-- gitster -- in commit 7949837 ,2016 年 7 月 6 日)

reflog : continue walking the reflog past root commits reflog : 继续遍历reflog越过根提交

If a repository contains more than one root commit, then its HEAD reflog may contain multiple "creation events", ie entries whose "from" value is the null sha1.如果存储库包含多个根提交,则其 HEAD 引用日志可能包含多个“创建事件”,即“from”值为空 sha1 的条目。
Listing such a reflog currently stops prematurely at the first such entry, even when the reflog still contains older entries.列出这样的 reflog 当前在第一个这样的条目处过早停止,即使 reflog 仍然包含较旧的条目。
This can scare users into thinking that their reflog got truncated after ' git checkout --orphan '.这可能会吓到用户认为他们的 reflog 在“ git checkout --orphan ”之后被截断了。

Continue walking the reflog past such creation events based on the preceeding reflog entry's "new" value.根据前面的引用日志条目的“新”值,继续遍历引用日志经过此类创建事件。

I typically find the git reflog output to be confusing.我通常发现git reflog输出令人困惑。 It's much easier for me to understand a commit graph from git log --graph --reflog .对我来说,从git log --graph --reflog理解提交图要容易git log --graph --reflog Overriding the log format to show only commit summaries also can make the graph easier to follow:覆盖log格式以仅显示提交摘要也可以使图表更易于理解:

$ git config --global alias.graph \
    "log --graph --all --format='%h %s%n        (%an, %ar)%d' --abbrev-commit"

$ git graph --reflog

* f06abeb Add feature
|         (Sue Dakota, 4 days ago) (HEAD -> master)
* f126291 Fix the build
|         (Oski M. Wizard, 5 days ago) (origin/master, master)
* 3c4fb9c Move fast, break things
|         (Alyssa P. Hacker, 5 days ago)
| * e3124bf fixup! More work for feature
| |         (Sue Dakota, 4 days ago)
| | * 6a7a52e Lost commit
| |/          (Sue Dakota, 4 days ago)
| * 69d9438 More work for feature
| |         (Sue Dakota, 2 weeks ago)
| * 8f69aba Initial work for feature
|/          (Sue Dakota, 3 weeks ago)
* d824fa9 Fix warnings from the linter
|         (Theo Rhys Tudent, 4 weeks ago)
* 9f782b8 Fix test flakes
|         (Tess Driven, 5 weeks ago)

From that it's clear that e3124bf and 6a7a52e are unreferenced orphans, and there's context from their ancestor commits.很明显, e3124bf6a7a52e是未引用的孤儿,并且有来自它们祖先提交的上下文。

One good feature of git is that it detects corruption. git 的一个优点是它可以检测损坏。 However, it does not include error correction to protect from corruption.但是,它不包括错误纠正以防止损坏。

I hope that you have pushed the contents of this repository to another machine or that you have backups to recover the corrupted parts.我希望您已将此存储库的内容推送到另一台机器,或者您有备份来恢复损坏的部分。

I do not have any experience with git on windows but have never seen this sort of behavior with git on Linux or OS X.我对 Windows 上的 git 没有任何经验,但从未见过在 Linux 或 OS X 上使用 git 的这种行为。

If you wish to see only your orphaned commits, you can use the following commands (using a *nix shell):如果您只想查看孤立的提交,可以使用以下命令(使用 *nix shell):

# Save the output to a file since it might take a minute.
git fsck --unreachable > unreachable.txt
# Note unreachable.txt now includes all unreachable blobs, trees, and commits.
cat unreachable.txt | grep commit

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

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