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

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 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

其他回答

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

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

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

修复git对象错误解决方案 只需运行以下命令

find .git/objects/ -size 0 -exec rm -f {} \;
git fetch origin



Refresh index: 100% (5322/5322), done.
error: object file .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c is empty
error: object file .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c is empty
error: object file .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c is empty
fatal: loose object d4f989f0e5f4613a3cbba836b3c77dfb71a7a56c (stored in .git/objects/d4/f989f0e5f4613a3cbba836b3c77dfb71a7a56c) is corrupt

在一个脚本中

#! /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服务器(总共9个文件),这样就解决了这个问题。

因为我必须定期重启我的虚拟机,不知何故这个问题经常发生在我身上。几次之后,我意识到我不能每次都重复Nathan Vanhoudnos所描述的过程,尽管它总是有效的。然后我想出了以下更快的解决方案。

步骤1

将整个存储库移动到另一个文件夹。

mv current_repository temp_repository

步骤2

再次从原点克隆存储库。

git clone source_to_current_repository.git

步骤3

删除新存储库下的所有内容,除了.git文件夹。

步骤4

将temp_repository中的所有内容移到新的存储库中,除了.git文件夹。

步骤5

删除temp_repository,就完成了。

几次之后,我相信你可以很快地完成这些步骤。