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

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存储库的最快方法,使用以下命令(在包含.git文件夹的Git工作目录中执行):

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

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

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

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

其他回答

我在使用虚拟机时经常遇到这个问题。

对我来说,以下工作:

cd /path/to/your/project
rm -rf .git

如果你想为自己节省一些下载-进入你的文件资源管理器,删除文件夹中已经提交的所有文件,并保留在你的/vendor和/node_modules(我使用PHP Composer和npm)文件夹中。

然后创建一个新的存储库:

git init

添加遥控器

git remote add origin ssh://git@github.com/YourUsername/repoName.git

然后去取树枝/全部

git fetch origin somebranch

大家来看看

git checkout somebranch

那么你应该在错误之前的点。

在我的例子中,出现这个错误是因为我正在输入提交消息,而我的笔记本关机了。

我执行了以下步骤来修复错误:

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操作期间没电了。嘘。

我没有备份。(注意:Ubuntu One不是Git的备份解决方案;它将有助于覆盖你的健全的存储库与你的损坏。)

对于Git向导,如果这是一种糟糕的修复方法,请留下评论。然而,它确实对我有用……至少暂时是这样。

第一步:备份。git文件夹(事实上我在每一步之间都会做一些改变,但是会有一个新的copy-to名称,例如。git-old-1, .git-old-2,等等):

cd ~/workspace/mcmc-chapter
cp -a .git .git-old

步骤2:执行git fsck——full命令

git fsck --full

error: object file .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e is empty
fatal: loose object 8b61d0135d3195966b443f6c73fb68466264c68e (stored in .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e) is corrupt

步骤3:删除空文件。我想管他呢;反正是空白的。

rm .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e

rm: remove write-protected regular empty file `.git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e'? y

步骤3:重新执行git fsck命令。继续删除空文件。您也可以cd到.git目录并运行find。-type f -empty -delete -print删除所有空文件。最终Git开始告诉我它实际上在对对象目录做一些事情:

git fsck --full

Checking object directories: 100% (256/256), done.
error: object file .git/objects/e0/cbccee33aea970f4887194047141f79a363636 is empty
fatal: loose object e0cbccee33aea970f4887194047141f79a363636 (stored in .git/objects/e0/cbccee33aea970f4887194047141f79a363636) is corrupt

步骤4:在删除所有空文件后,我最终得到了git fsck实际运行:

git fsck --full

Checking object directories: 100% (256/256), done.
error: HEAD: invalid sha1 pointer af9fc0c5939eee40f6be2ed66381d74ec2be895f
error: refs/heads/master does not point to a valid object!
error: refs/heads/master.u1conflict does not point to a valid object!
error: 0e31469d372551bb2f51a186fa32795e39f94d5c: invalid sha1 pointer in cache-tree
dangling blob 03511c9868b5dbac4ef1343956776ac508c7c2a2
missing blob 8b61d0135d3195966b443f6c73fb68466264c68e
missing blob e89896b1282fbae6cf046bf21b62dd275aaa32f4
dangling blob dd09f7f1f033632b7ef90876d6802f5b5fede79a
missing blob caab8e3d18f2b8c8947f79af7885cdeeeae192fd
missing blob e4cf65ddf80338d50ecd4abcf1caf1de3127c229

步骤5:尝试git reflog。失败是因为我的头坏了。

git reflog

fatal: bad object HEAD

步骤6:谷歌。发现这些。手动获取reflog的最后两行:

tail -n 2 .git/logs/refs/heads/master

f2d4c4868ec7719317a8fce9dc18c4f2e00ede04 9f0abf890b113a287e10d56b66dbab66adc1662d Nathan VanHoudnos <nathanvan@gmail.com> 1347306977 -0400    commit: up to p. 24, including correcting spelling of my name
9f0abf890b113a287e10d56b66dbab66adc1662d af9fc0c5939eee40f6be2ed66381d74ec2be895f Nathan VanHoudnos <nathanvan@gmail.com> 1347358589 -0400    commit: fixed up to page 28

步骤7:注意,从步骤6中我们了解到HEAD当前指向最后一次提交。让我们看一下父commit

git show 9f0abf890b113a287e10d56b66dbab66adc1662d

commit 9f0abf890b113a287e10d56b66dbab66adc1662d
Author: Nathan VanHoudnos <nathanvan@XXXXXX>
Date:   Mon Sep 10 15:56:17 2012 -0400

    up to p. 24, including correcting spelling of my name

diff --git a/tex/MCMC-in-IRT.tex b/tex/MCMC-in-IRT.tex
index 86e67a1..b860686 100644
--- a/tex/MCMC-in-IRT.tex
+++ b/tex/MCMC-in-IRT.tex

它工作!

第8步:现在我们需要将HEAD指向9f0abf890b113a287e10d56b66dbab66adc1662d。

git update-ref HEAD 9f0abf890b113a287e10d56b66dbab66adc1662d

它没有抱怨。

步骤9:看看fsck说了什么:

git fsck --full

Checking object directories: 100% (256/256), done.
error: refs/heads/master.u1conflict does not point to a valid object!
error: 0e31469d372551bb2f51a186fa32795e39f94d5c: invalid sha1 pointer in cache-tree
dangling blob 03511c9868b5dbac4ef1343956776ac508c7c2a2
missing blob 8b61d0135d3195966b443f6c73fb68466264c68e
missing blob e89896b1282fbae6cf046bf21b62dd275aaa32f4
dangling blob dd09f7f1f033632b7ef90876d6802f5b5fede79a
missing blob caab8e3d18f2b8c8947f79af7885cdeeeae192fd
missing blob e4cf65ddf80338d50ecd4abcf1caf1de3127c229

第10步:缓存树中无效的sha1指针看起来像是来自一个(现在过时了)索引文件(源文件)。所以我杀了它,重置了存储库。

rm .git/index
git reset

Unstaged changes after reset:
M    tex/MCMC-in-IRT.tex
M    tex/recipe-example/build-example-plots.R
M    tex/recipe-example/build-failure-plots.R

步骤11:再次查看fsck…

git fsck --full

Checking object directories: 100% (256/256), done.
error: refs/heads/master.u1conflict does not point to a valid object!
dangling blob 03511c9868b5dbac4ef1343956776ac508c7c2a2
dangling blob dd09f7f1f033632b7ef90876d6802f5b5fede79a

悬垂的斑点不是错误。我不关心主人。u1conflict,现在它工作了,我不想再碰它了!

第12步:跟进我的本地编辑:

git status

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   tex/MCMC-in-IRT.tex
#    modified:   tex/recipe-example/build-example-plots.R
#    modified:   tex/recipe-example/build-failure-plots.R
#
< ... snip ... >
no changes added to commit (use "git add" and/or "git commit -a")


git commit -a -m "recovering from the git fiasco"

[master 7922876] recovering from the git fiasco
 3 files changed, 12 insertions(+), 94 deletions(-)

git add tex/sept2012_code/example-code-testing.R
git commit -a -m "adding in the example code"

[master 385c023] adding in the example code
 1 file changed, 331 insertions(+)
 create mode 100644 tex/sept2012_code/example-code-testing.R

我修复了我的git错误:对象文件是空的:

保存我上次成功提交/推送后编辑的所有文件的副本, 删除并重新克隆我的存储库, 用我编辑过的文件替换旧文件。