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

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文件损坏后,我也有同样的问题。

第一步,从项目的根文件夹。

find .git/objects -type f -empty -delete

然后是西梅和fetch…

git prune
git fetch --all --prune

然后做了一些回滚,然后就开始工作了。

如果你有一个旧的备份,并且很着急:

为当前的Git-broken项目路径做一个新的备份。

移动你的.git到垃圾桶(永远不要删除) 从旧备份中复制.git Git pull(会产生合并冲突) 将你所有的源文件(你放入Git中的所有内容)移到垃圾:./src(从不删除) 从新的备份中复制所有源代码(放入Git中的所有内容) 接受所有“合并”在git gui,推送和…拍拍手!

在一个脚本中

#! /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"

复制所有文件(在包含.git文件夹的文件夹中)到备份,删除所有文件,然后重新启动。确保你手边有Git遥控器:

git remote -v
 origin    git@github.com:rwldrn/idiomatic.js.git (fetch)
 origin    git@github.com:rwldrn/idiomatic.js.git (push)

Then

mkdir mygitfolder.backup
cp mygitfolder/* mygitfolder.backup/
cd mygitfolder
rm -r * .git*
git init
git remote add origin git@github.com:rwldrn/idiomatic.js.git

然后手动合并任何新文件,并试着让你的电脑开着。

Git对象文件已经损坏(正如在其他回答中指出的那样)。这可能发生在机器崩溃等情况下。

我也有同样的问题。在阅读了这里的其他顶级答案之后,我发现了修复损坏的Git存储库的最快方法,使用以下命令(在包含.git文件夹的Git工作目录中执行):

(请务必先备份Git存储库文件夹!)

find .git/objects/ -type f -empty | xargs rm
git fetch -p
git fsck --full

这将首先删除导致整个存储库损坏的所有空对象文件,然后从远程存储库获取缺失的对象(以及最新的更改),然后执行完整的对象存储检查。在这一点上,它应该成功而没有任何错误(尽管仍然可能有一些警告!)

PS:这个答案表明你有一个Git存储库的远程副本 在某个地方(例如在GitHub上),损坏的存储库是本地存储库,它绑定到仍然完整的远程存储库。如果不是这样,那么不要尝试用我推荐的方法来修复它。