当我尝试提交更改时,我得到这个错误:

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 fsck检测到的各种空文件,然后运行一个简单的git拉。

我感到失望的是,现在即使文件系统实现了日志记录和其他“事务性”技术来保持文件系统正常运行,Git也会因为设备上的电源故障或空间问题而进入损坏状态(并且无法自行恢复)。

其他回答

移动你的应用文件夹做一个备份,即mv app_folder app_folder_bk(它就像一个git stash) Git克隆your_repository 最后,打开一个合并工具(我在Linux上使用Meld diff查看器,在Windows上使用WinMerge),从右边(app_folder_bk)复制更改到左边(new app_folder)(这就像一个git stash应用)。

这是所有。也许这不是最好的方法,但我认为它很实用。

如果你在github.com上的公共存储库正常工作,但你的本地存储库损坏,这里有一种解决问题的方法。请注意,您将丢失在本地存储库中所做的所有提交。

我有一个本地存储库,给我这个对象空错误,github.com上的同一个存储库,但没有这个错误。因此,我所做的只是从GitHub克隆存储库,然后从损坏的存储库中复制所有内容(除了.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 fsck检测到的各种空文件,然后运行一个简单的git拉。

我感到失望的是,现在即使文件系统实现了日志记录和其他“事务性”技术来保持文件系统正常运行,Git也会因为设备上的电源故障或空间问题而进入损坏状态(并且无法自行恢复)。

在一个脚本中

#! /bin/sh

# Save Git data
cp -r .git gitold

# Remove all empty Git object files
find .git -type f -empty -delete -print

# Get the current branch name
branchname=$(git branch --show-current)

# Get the latest commit hash
commit=$(tail -2 .git/logs/refs/heads/$branchname | awk '{ print $2 }' | tr -d '[:space:]')

# Set HEAD to this latest commit
git update-ref HEAD $commit

# Pull the latest changes on the current branch (considering remote is origin)
git pull origin $branchname

echo "If everything looks fine you remove the git backup running :\n\
      $ rm -rf gitold \n\
Otherwise restore it with: \n\
      $ rm -rf .git; mv gitold .git"