当我尝试提交更改时,我得到这个错误:
error: object file .git/objects/31/65329bb680e30595f242b7c4d8406ca63eeab0 is empty
fatal: loose object 3165329bb680e30595f242b7c4d8406ca63eeab0 (stored in .git/objects/31/65329bb680e30595f242b7c4d8406ca63eeab0) is corrupt
我尝试了我得到的 git fsck:
error: object file .git/objects/03/dfd60a4809a3ba7023cbf098eb322d08630b71 is empty
fatal: loose object 03dfd60a4809a3ba7023cbf098eb322d08630b71 (stored in .git/objects/03/dfd60a4809a3ba7023cbf098eb322d08630b71) is corrupt
如何解决这个错误?
在我的例子中,保存本地提交历史对我来说并不重要。所以,如果这也适用于你,你可以这样做,作为上述解决方案的快速替代方案:
基本上,您只需将损坏的.git/目录替换为一个干净的目录。
假设包含已损坏Git文件的项目位于以下目录:projects/corrupt_git/
Cp projects/corrupt_git projects/backup -(可选)进行备份
git克隆[repo URL] projects/clean_git -这样你就可以得到projects/clean_git
Rm -rf corrupt_git/。Git / -删除损坏的。Git文件夹
mv clean_git /。Git / corrupt_git/ -将干净的Git移动到corrupt_git/.git
Git的状态在projects/corrupt_git -以确保它工作
这种情况也经常发生在我身上。我没有制定具体何时发生这种情况的协议,但我怀疑每当我的虚拟机(VM)“意外”存在时就会发生这种情况。如果我关闭虚拟机窗口(我使用的是Ubuntu 18.04 (Bionic Beaver))并重新开始,事情总是(?)工作。但是如果当我的笔记本电脑关闭(Windows主机系统)时,虚拟机窗口仍然打开,那么我经常遇到这个问题。
对于这里给出的所有答案:
thank you - they are very useful; I usually save a local copy of my code, restore the repository from remote, and move the backup copy back into the local folder.
as the underlying problem is not really a Git issue, but rather a VM and/or Linux issue, I wonder if there shouldn't be a way to cure the reason rather the symptoms? Doesn't this kind of error indicate that some file system changes are not "applied" in any reasonable time, but only cached? (see for example Are file edits in Linux directly saved into disk?) -- to me it appears as if virtual Linux machines don't fsynch their stuff frequently enough. Whether this is an issue of Oracle's VirtualBox (which otherwise works very nicely) or of the guest file system, or of some settings, which we all overlook, is beyond my expertise. But I would be happy if someone could shed light on this.
在我的例子中,出现这个错误是因为我正在输入提交消息,而我的笔记本关机了。
我执行了以下步骤来修复错误:
git checkout -b backup-branch # Create a backup branch
git reset --hard HEAD~4 # Reset to the commit where everything works well. In my case, I had to back four commits in the head, that is until my head be at the point before I was typing the commit message. Before doing this step, copy the hash of the commits you will reset. In my case I copied the hash of the four last commits.
git cherry-pick <commit-hash> # Cherry pick the reset commits (in my case are four commits, so I did this step four times) from the old branch to the new branch.
git push origin backup-branch # Push the new branch to be sure everything works well
git branch -D your-branch # Delete the branch locally ('your-branch' is the branch with problem)
git push origin :your-branch # Delete the branch from remote
git branch -m backup-branch your-branch # Rename the backup branch to have the name of the branch that had the problem
git push origin your-branch # Push the new branch
git push origin :backup-branch # Delete the backup branch from remote