计算机死机后 Git 存储库损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15317072/
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 15:42:42  来源:igfitidea点击:

Git repository broken after computer died

git

提问by vainolo

My computer went dead and now one of my git repositories is broken. When I try to checkout master it tells me:

我的电脑坏了,现在我的一个 git 存储库坏了。当我尝试结帐大师时,它告诉我:

warning: ignoring broken ref refs/heads/master.
error: Your local changes to the following files would be overwritten by checkout:
        com.vainolo.jdraw2d.releng.p2/pom.xml
Please, commit your changes or stash them before you can switch branches.
Aborting

When I execute git stashI get:

当我执行git stash我得到:

fatal: bad revision 'HEAD'
fatal: bad revision 'HEAD'
fatal: Needed a single revision
You do not have the initial commit yet

So... what can I do?

所以……我能做什么?

UpdateOutput of git reflog:

更新输出git reflog

fatal: bad default revision 'HEAD'

Not very promising... Output of git fsck:

不是很有希望......的输出git fsck

error: Invalid HEAD
Checking object directories: 100% (256/256), done.
error: unable to unpack 59551f96b4e87a1c14293c19eb548ce6fa1f196f header
error: inflateEnd: stream consistency error (no message)
fatal: loose object 59551f96b4e87a1c14293c19eb548ce6fa1f196f (stored in .git/objects/59/551f96b4e87a1c14293c19eb548ce6fa1f196f) is corrupt

回答by Julian Frumar

I managed to recover through:

我设法通过以下方式恢复:

rm .git/refs/remotes/origin/HEAD
git fetch --all

回答by kynan

Start by following the steps suggested in Recovering broken git repository:

按照恢复损坏的 git 存储库中建议的步骤开始:

  • check whether .git/refsstill contains anything useful
  • check git reflogand failing that the contents of .git/logs/refs/heads/masteror whatever branch you were on last
  • run git fsck, potentially with --unreachableor --lost-found
  • 检查是否.git/refs仍然包含任何有用的东西
  • 检查git reflog并失败.git/logs/refs/heads/master您上次所在的分支或任何分支的内容
  • 运行git fsck,可能与--unreachable--lost-found

This will hopefully allow you to figure out what the masterref should be so you can restore it (i.e. cat the correct SHA1 into .git/refs/heads/master).

这有望让您弄清楚masterref 应该是什么,以便您可以恢复它(即,将正确的 SHA1 转换为.git/refs/heads/master)。

In case any object contained in that commit is genuinely corrupted you can't restore your HEADcommit unfortunately. Assuming your working tree and/or index are intact you can try a git reset --soft(or failing that a git reset) to the previous commit and then re-do the commit. Avoid any operations that change your working tree s.a. git checkout -for git reset --hard.

如果该提交中包含的任何对象真正损坏,您将无法恢复您的HEAD提交。假设您的工作树和/或索引完好无损,您可以尝试 a git reset --soft(或失败 a git reset)到先前的提交,然后重新提交。避免任何会更改您的工作树 sagit checkout -fgit reset --hard.

回答by Carlton

I had a similar issue following a blue screen of death on windows 8.1

在 Windows 8.1 上出现蓝屏死机后,我遇到了类似的问题

I had a file in this location...

我在这个位置有一个文件...

C:\www\<project>\.git\refs\remotes\origin\<problem-branch>

C:\www\<project>\.git\refs\remotes\origin\<problem-branch>

And it was empty whereas the other branch files in this folder has long strings inside of them.

它是空的,而这个文件夹中的其他分支文件里面有很长的字符串。

NB I didn't have any changes/commits

注意我没有任何更改/提交

  • I backed up the <problem-branch>file
  • Deleted the file
  • git fetch --allto get the branch again
  • 我备份了<problem-branch>文件
  • 删除了文件
  • git fetch --all再次获得分支

Then the tab auto completion started working again

然后选项卡自动完成再次开始工作

回答by alsotang

If there is not many modified files, I think the convient way to solve this problem is:

如果修改的文件不多,我认为解决这个问题的便捷方法是:

  1. backup the files you modified in the repo
  2. remove your existing repo
  3. re-clone it from server
  4. paste the files from step 1 to the repo, and git commit -a
  1. 备份你在 repo 中修改过的文件
  2. 删除您现有的回购
  3. 从服务器重新克隆它
  4. 将第 1 步中的文件粘贴到 repo,然后 git commit -a

回答by Manos Gaitanakis

i managed to solve this with deleting master file in git\refs\heads directory

我设法通过删除 git\refs\heads 目录中的主文件来解决这个问题

回答by j4v1

After a computed freeze and crash, my git branch was damaged with the message: git fatal: your current branch appears to be broken. I could not do anything.

一个计算冻结和崩溃后,我的git分行与消息损坏: git fatal: your current branch appears to be broken。我什么也做不了。

After doing git fsckmentioned that the branch had an error: Invalid HEAD. refs/heads/<branch>had an invalid sha1 pointer.

完成后git fsck提到该分支有一个error: Invalid HEAD. refs/heads/<branch>有一个invalid sha1 pointer.

Upon following the options here, I opened .git/refs/heads/<branch>in an notepad++ editor, and each of the sha1 characters were NUL.

按照这里的选项,我.git/refs/heads/<branch>在记事本++编辑器中打开,每个 sha1 字符都是NUL.

Fortunately I only needed to reset the branch to remote state, and that was on a bitbucket repo. I grabbed the sha1 from the tip of the remote repo and copied to the .git/refs/heads/<branch>saved it, then did a git reset --hard HEAD, and everything back to normal.

幸运的是,我只需要将分支重置为远程状态,这在一个 bitbucket 存储库中。我从远程仓库的尖端抓取了 sha1 并复制到.git/refs/heads/<branch>保存的它,然后做了一个git reset --hard HEAD,一切恢复正常。

回答by Markus Palcer

I was idiot enough to forget to push and my computer crashed while performing a commit. I could recover everything but the last commit though by opening .git/logs/refs/heads/

我太白痴了,忘记推送了,我的电脑在执行提交时崩溃了。我可以通过打开 .git/logs/refs/heads/ 恢复除了最后一次提交之外的所有内容

This file contains all commits (with their SHAs) to the branch, what I did to recover was:

该文件包含对分支的所有提交(及其 SHA),我为恢复所做的工作是:

  • Back up latest changes to a temp folder
  • move to a "clean slate"
    • git checkout master
    • git reset --hard
  • checkout the second to last commit in the log
  • create a branch from this detached head
  • PUSH
  • Restore the latest changes
  • Commit again
  • 将最新更改备份到临时文件夹
  • 转移到“干净的石板”
    • git checkout master
    • git reset --hard
  • 检出日志中倒数第二个提交
  • 从这个分离的头创建一个分支
  • 恢复最新更改
  • 再次提交

So even when you make a dumb mistake, you're not immediately taken back by a whole day of work with git :)

所以即使你犯了一个愚蠢的错误,你也不会立即被一整天的 git 工作收回:)

回答by aaronwbrown

I know it's a too-late response, but I was getting this error because I did not have an origin/head. You can find this out by running git branch -r. If you do not see your origin/headpointing to a remote origin, you can set this by running git remote set-head origin {{your branch name}}.

我知道这是一个为时已晚的响应,但我收到此错误是因为我没有origin/head. 您可以通过运行找到这一点git branch -r。如果您没有看到您origin/head指向远程源,您可以通过运行来设置它git remote set-head origin {{your branch name}}

Now run git branch -ragain, and you should see something like this: origin/HEAD -> origin/develop

现在git branch -r再次运行,您应该会看到如下内容: origin/HEAD -> origin/develop

I hope this helps anyone else who's running into this issue.

我希望这可以帮助其他遇到此问题的人。

回答by Keon Sadatian

I could not checkout my master branch due to the cannot lock ref error. I ended up deleting: .git/refs/remotes/origin/HEAD.git/refs/remotes/origin/master

由于无法锁定引用错误,我无法检出我的主分支。我最终删除了: .git/refs/remotes/origin/HEAD.git/refs/remotes/origin/master

and calling this git command:

并调用此 git 命令:

git fetch --all

回答by Hammad Khan

My computer crashed two times and a result, my git repository got broken locally. I could not pull my changes, it asked to set remote origin but it did not work in gitKraken.

我的电脑崩溃了两次,结果是我的 git 存储库在本地损坏了。我无法拉动我的更改,它要求设置远程源,但它在 gitKraken 中不起作用。

On my command prompt, I was getting this error enter image description here

在我的命令提示符下,我收到此错误 在此处输入图片说明

I knew that my references are broken and needs to be fixed. What I had to do is, to git bash (In SourceTree click on "terminal"). Then navigate to references folder like this

我知道我的参考资料已损坏,需要修复。我必须做的是 git bash (在 SourceTree 中单击“终端”)。然后导航到这样的引用文件夹

cd .git
cd refs
cd remotes
cd origin

there will be a file name masterthere, use lsto see what is in the directory. Then simply delete it using rm master

那里会有一个文件名masterls用来查看目录中的内容。然后只需使用 rm master 删除它

bam, the corrupted file is gone. Now if you issue git branch command -a, it would output this

bam,损坏的文件不见了。现在如果你发出 git branch 命令 -a,它会输出这个

$ git branch -a
* master
  remotes/origin/master (this in red color -scary :) )

Then issue this command, and it will fix your references

然后发出此命令,它将修复您的引用

$ git remote set-head origin master

To sum it up, if you would try to pull the remote, it should show blank remote name which has been fixed.

总而言之,如果您尝试拉动遥控器,它应该显示已修复的空白遥控器名称。

enter image description here

在此处输入图片说明