修复 error: object file .git/objects/... is empty

2023-05-25 01:48:14 +0800 CST 3 min read views: 329

通常出现这样的错误

➜  my-repo git:(main) git status
error: object file .git/objects/13/e124a17295dde35867fe657f96e128d4a5f808 is empty
error: object file .git/objects/13/e124a17295dde35867fe657f96e128d4a5f808 is empty
fatal: loose object 13e124a17295dde35867fe657f96e128d4a5f808 (stored in .git/objects/13/e124a17295dde35867fe657f96e128d4a5f808) is corrupt

备份.git

➜  my-repo git:(main) cp -a .git .git-old

删除空文件

➜  my-repo git:(main) cd .git/

# 删除当前文件目录及子目录下的所有空文件
➜  .git git:(main) find . -type f -empty -delete -print
./objects/c8/f3806e754a12c122dd9cfc87f133bc4cd18e6f
./objects/a2/7eafde0a9d97e6de218962cb9aba5a5e54e6d6
./objects/c0/52d5294bef1108256039ddf1e0985ab4443ed1
./objects/5f/1588036b832d3dc1cc41a29eabd462cc20799b
./objects/9f/0ccd4ab6662d21658a631b51fa461f3201f0e7
./objects/84/ce7781b75a21b88ad42feb67cdeb6d6f960d52
./objects/d1/faf3c29ec08a930c05663eea91bdfe05b6536f
./objects/cd/8337d2b5424cf90a2281466e24a9138df89645
./objects/54/3348ee23b3544aa6df066e6632a03f14363ca5
./objects/63/85e6fe1363e1ca7b0961015d26a92afaae93f8
./objects/c2/495a70e40962c6a61dd1d997fda8ac4953c061
./objects/13/e124a17295dde35867fe657f96e128d4a5f808

验证

➜  my-repo git:(main) git fsck --full                                                       
                                                                                               
Checking object directories: 100% (256/256), done.    
Checking objects: 100% (433/433), done.                                                        
error: refs/heads/main: invalid sha1 pointer 13e124a17295dde35867fe657f96e128d4a5f808
error: refs/remotes/origin/HEAD: invalid sha1 pointer 13e124a17295dde35867fe657f96e128d4a5f808
error: refs/remotes/origin/main: invalid sha1 pointer 13e124a17295dde35867fe657f96e128d4a5f808
error: HEAD: invalid sha1 pointer 13e124a17295dde35867fe657f96e128d4a5f808
error: 543348ee23b3544aa6df066e6632a03f14363ca5: invalid sha1 pointer in cache-tree
dangling blob 22f8d9b06a4d82af1c42ebc3270cf0380b1adbe4
dangling blob 6d50d34c3cf7c41ecabcab34354b6f92a77fb9be
  • 出现错误,与远程同步一下
➜  my-repo git:(main) git pull                     
remote: Enumerating objects: 137, done.                                                        
remote: Counting objects: 100% (137/137), done.                                                
remote: Compressing objects: 100% (63/63), done.      
Receiving objects: 100% (113/113), 23.75 KiB | 352.00 KiB/s, done.
remote: Total 113 (delta 70), reused 86 (delta 44), pack-reused 0
Resolving deltas: 100% (70/70), completed with 16 local objects.
Already up to date.                                                                            
➜  my-repo git:(main) ✗ git clean -fd                                                       
Removing .git-old/    

# 谨慎操作!!!                                                                         
➜  my-repo git:(main) git reset --hard origin/main 

查看当前 git 状态

➜  my-repo git:(main) git status     
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean