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

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

其他回答

这里有一个非常简单和快速的方法来处理这个问题,如果你有一个本地回购和所有你需要的分支和提交,如果你可以创建一个新的回购(或删除服务器的回购并在它的位置上创建一个新的):

在服务器上创建一个新的空回购(或删除旧的回购并在其位置上创建一个新的回购) 将本地副本的远程URL更改为指向新回购的远程URL。 将所有分支从本地回购推到新的服务器回购。

这将保存您在本地回购中拥有的所有提交历史和分支。

如果你在回购上有合作者,那么我认为在很多情况下,你的合作者所要做的就是改变他们本地回购的远程URL,并有选择地推送任何他们拥有的服务器没有的提交。

当我遇到同样的问题时,这个解决方案对我很有效。我有一个合作者。在我将本地回购推到新的远程回购后,他只是将本地回购更改为指向远程回购URL,一切都正常工作。

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

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

在一个脚本中

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

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

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

我假设你有一个遥控器,所有相关的变化已经推送到它。我不关心本地更改,只是希望避免删除和重新克隆大型存储库。如果您确实有重要的局部更改,您可能需要更加小心。

我的笔记本电脑死机后也遇到了同样的问题。 可能是因为它是一个很大的存储库,我有相当多的损坏的对象文件,当调用git fsck—full时,每次只出现一个,所以我写了一个小的shell一行程序来自动删除其中一个:

$ sudo rm ' git fsck,满2 > & 1 | grep oe - m 1 " . /对象/ [0-9a-f] {2} [0-9a-f] *”的

2>&1将错误消息重定向到标准输出,以便能够对其进行grep 使用的Grep选项: -o只返回行中实际匹配的部分 -E启用高级正则表达式 -m 1确保只返回第一个匹配项 [0-9a-f]{2}匹配0到9之间的任意字符,如果a和f同时出现,则匹配a和f [0-9a-f]*匹配0到9之间且a和f同时出现的任意数量的字符

它一次仍然只删除一个文件,所以你可能想在循环中调用它,就像:

$ while为true;做sudo rm的git fsck,满2 > & 1 | grep oe - m 1 " . /对象/ [0-9a-f] {2} [0-9a-f] *”的;完成

这样做的问题是,它不再输出任何有用的东西,所以您不知道它什么时候完成(一段时间后它应该不会做任何有用的事情)。

为了“修复”这个问题,我只需要在每一轮之后调用git fsck—full,如下所示: $ while为true;做sudo rm的git fsck,满2 > & 1 | grep oe - m 1 " . /对象/ [0-9a-f] {2} [0-9a-f] *”的;Git FSCK—满;完成

它现在的速度大约是原来的一半,但它确实输出了它的“状态”。

在这之后,我玩了一些建议在这里的答案,最后得到了一个点,我可以得到藏匿和藏匿掉很多破碎的东西。

第一个问题解决了

后来我还是有这样一个问题: 无法解析引用'refs/remotes/origin/$branch':引用破裂,可以通过 $ rm \repo.git\refs\remotes\origin$branch

$ git fetch

然后我做了一个 $ git gc -prune=现在

$ git远程修剪来源

为了更好地衡量

Git reflog expire -stale-fix -all

当运行git fsck——full时,清除错误:HEAD: invalid reflog entry $blubb。