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

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 status

显示空/损坏的目标文件

rm .git/objects/08/3834cb34d155e67a8930604d57d3d302d7ec12

删除它

git status

我得到了致命的:坏对象头部消息

rm .git/index

我移除重置的索引。

git reset

无法解析对象“HEAD”。

git status
git pull

看看发生了什么

tail -n 2 .git/logs/refs/heads/MY-CURRENT-BRANCH

它输出log分支的最后两行,tail - n2,来显示最后两次提交哈希值。

git update-ref HEAD 7221fa02cb627470db163826da4265609aba47b2

我选择最后一个提交哈希

git status

它显示我所有的文件都已删除,因为我删除了.git/index文件

git reset

继续复位

git status

验证我的修复

其他回答

复制所有文件(在包含.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

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

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

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

因为我必须定期重启我的虚拟机,不知何故这个问题经常发生在我身上。几次之后,我意识到我不能每次都重复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,就完成了。

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

在一个脚本中

#! /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 stash
git checkout master
cd .git/ && find . -type f -empty -delete
git branch your-branch-name -D
git checkout -b your-branch-name
git stash pop