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

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

如何解决这个错误?


当前回答

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

对我来说,以下工作:

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对象错误解决方案 只需运行以下命令

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

我从一个干净的分支机构退房后也遇到了同样的问题。

过了一段时间,我在master中发现了很多修改过的文件。我不知道为什么他们一直在那里,从一个干净的分支切换。不管怎样,因为修改后的文件对我来说没有意义,我只是把它们藏起来,错误就消失了。

git:(master) git stash

事实上,我也有同样的问题。

在尝试此操作之前,请准备一份代码副本。

我刚刚做了 git重置头~

我的最后一次承诺也没有兑现。然后我又犯了一次,问题就解决了!