How do I deal with corrupted Git object files?
Asked Answered
R

9

100

I did a Git pull when I was near my quota, and as a result (so I think), got a corrupted file:

$ git pull
walk dffbfa18916a9db95ef8fafc6d7d769c29a445aa
fatal: object d4a0e7599494bfee2b5351113895b43c351496b3 is corrupted

$ git fsck --full
bad sha1 file: .git/objects/66/b55c76947b1d38983e0944f1e6388c86f07a1b.temp
fatal: object d4a0e7599494bfee2b5351113895b43c351496b3 is corrupted

$ git cat-file -t d4a0e7599494bfee2b5351113895b43c351496b3
error: unable to find d4a0e7599494bfee2b5351113895b43c351496b3
fatal: git cat-file d4a0e7599494bfee2b5351113895b43c351496b3: bad file

How can I solve this corruption?

.git/objects/66/b55c76947b1d38983e0944f1e6388c86f07a1b.temp was zero bytes; deleting it did nothing to solve my problem (same errors).

Richmond answered 6/11, 2010 at 3:20 Comment(10)
The contents must sha1 sum to the filename. There's no way to restore the contents based on the hash, so unless git has some kind of redundancy built in for this exact situation (I can't say it doesn't) I'd say you need to re-fetch or clone the remote again.Florey
You said you deleted it - did you try pulling again after that? A zero size temporary object smacks of an aborted transfer...Anomie
The output of find . -name d4a0e7599494bfee2b5351113895b43c351496b3 is nothing. Deleting the sha1 file then pulling just results in the empty sha1 file being generated.Richmond
Also $ git cat-file -t d4a0e7599494bfee2b5351113895b43c351496b3's output is: error: unable to find d4a0e7599494bfee2b5351113895b43c351496b3 fatal: git cat-file d4a0e7599494bfee2b5351113895b43c351496b3: bad fileRichmond
That's not where that object would be stored. It'd be .git/objects/d4/a0e7599.... You could try backing up and removing that object (and removing any associated temporary files) and pulling again.Anomie
Jefromi: Thanks! That got me most of the way there, I think. When doing the pull, I get: error: Entry 'myFile.h' not uptodate. Cannot merge. fatal: merging of trees c088e794c4820171e75f829957f7c402b228aaa7 and 66b55c76947b1d38983e0944f1e6388c86f07a1b failedRichmond
@Mike: Hm. Maybe a git reset to get your index back into a good state?Anomie
git stash ; git pull ; git stash pop did the trick.Richmond
@Mike: Oh, you just had local modifications, nothing scary. I didn't remember you could get that low-level of a merge failure message!Anomie
Jefromi: If you write up an answer, I'll mark it as accepted. Thanks for your help!Richmond
A
55

In general, fixing corrupt objects can be pretty difficult. However, in this case, we're confident that the problem is an aborted transfer, meaning that the object is in a remote repository, so we should be able to safely remove our copy and let git get it from the remote, correctly this time.

The temporary object file, with zero size, can obviously just be removed. It's not going to do us any good. The corrupt object which refers to it, d4a0e75..., is our real problem. It can be found in .git/objects/d4/a0e75.... As I said above, it's going to be safe to remove, but just in case, back it up first.

At this point, a fresh git pull should succeed.

...assuming it was going to succeed in the first place. In this case, it appears that some local modifications prevented the attempted merge, so a stash, pull, stash pop was in order. This could happen with any merge, though, and didn't have anything to do with the corrupted object. (Unless there was some index cleanup necessary, and the stash did that in the process... but I don't believe so.)

Anomie answered 6/11, 2010 at 5:6 Comment(1)
This might result in error: refs/heads/branch does not point to a valid object!. Make sure to backup.Drawplate
M
77

You can use "find" for remove all files in the /objects directory with 0 in size with the command:

find .git/objects/ -size 0 -delete

Backup is recommended.

Moritz answered 27/7, 2015 at 7:53 Comment(2)
hmm, that helped, after suspend I've got a couple of objects empty and removing them allowed me to fetch real objects from remote repo. Luckily I did push just before suspend. Yet to see what's going on with files I did not commit yet, hopefully they were synced to disk before suspend. WARNING, better save the hashes you delete if further inspection is needed.Benzidine
find .git/objects/ -type f -size 0 -deleteSogdian
A
55

In general, fixing corrupt objects can be pretty difficult. However, in this case, we're confident that the problem is an aborted transfer, meaning that the object is in a remote repository, so we should be able to safely remove our copy and let git get it from the remote, correctly this time.

The temporary object file, with zero size, can obviously just be removed. It's not going to do us any good. The corrupt object which refers to it, d4a0e75..., is our real problem. It can be found in .git/objects/d4/a0e75.... As I said above, it's going to be safe to remove, but just in case, back it up first.

At this point, a fresh git pull should succeed.

...assuming it was going to succeed in the first place. In this case, it appears that some local modifications prevented the attempted merge, so a stash, pull, stash pop was in order. This could happen with any merge, though, and didn't have anything to do with the corrupted object. (Unless there was some index cleanup necessary, and the stash did that in the process... but I don't believe so.)

Anomie answered 6/11, 2010 at 5:6 Comment(1)
This might result in error: refs/heads/branch does not point to a valid object!. Make sure to backup.Drawplate
S
14

Recovering from Repository Corruption is the official answer.

The really short answer is: find uncorrupted objects and copy them.

Swine answered 6/11, 2010 at 4:38 Comment(1)
This is not particularly helpful. The corrupt object in question is almost certainly not one which needs recovering; it's one which was partially fetched from a remote.Anomie
W
6

For anyone stumbling across the same issue:

I fixed the problem by cloning the repo again at another location. I then copied my whole src dir (without .git dir obviously) from the corrupted repo into the freshly cloned repo. Thus I had all the recent changes and a clean and working repository.

Windmill answered 4/11, 2020 at 16:29 Comment(0)
A
5

Simple solution:

  1. Delete .git folder
  2. clone your project from github git clone url
  • (clone outside of your working folder, you just need .git folder)
  1. .git folder cut and paste in your working folder
  2. Now you can add git commit
git add .
git commit -m 'update-project'
git push
Abstergent answered 16/5, 2022 at 9:45 Comment(3)
For me, I think PHPStorm corrupted .git on my machine. Thanks for the simple solution. I was getting nowhere trying the various suggestions, and didn't want to start again from a clone because there were lots of untracked per-installation files which needed to be added, but your "clone and swap" solution was what I was looking for.Excellence
Should also mention, I just did a git status and that refreshed all the indices, so didn't have to add / commit / push.Excellence
As @YiminRong said, it is only necessary to execute git status after you paste in your new .git folder. Thanks for the solutionFanchie
M
1

The only thing that worked for me was to delete the git repo and configure it again:

rm -rf .git
git remote set-url origin <repo-url>
Maemaeander answered 15/5, 2021 at 15:44 Comment(1)
You may also need to run git init before the git remote command.Strappado
R
0

what I do is: shows what's in this repo, make sure you see a .git folder, (move first to root directory)

ls -a             
remove the .git folder
sudo rm -r .git             

make and move to a new dir where you can temporary store a new clone of the repo where you are working on.

cd ../
mkdir t007
cd t007

re-clone the repo where you are working on, and navigate to it

git clone cloneSshUrlOfGitRepo
cd nameOfRepo  

move the frech .git folder from the just cloned repo to the repo where you had a corrupt object, and where you deleted the .git folder. (after this stap your old repo should be up and running again, because you replaced the corrupt .git folder with a new one who should be working).

mv .git ../../nameOfRepo

now you should be able to remove the new cloned repo (because you don't need it anymore, we already moved its working .git folder in the previus step)

cd ../
sudo rm -r nameOfRepo

now you can move back to your repo where you were working on before you had this issue.

cd ../nameOfRepo
Registration answered 16/4, 2022 at 17:3 Comment(0)
A
0

I have solved this issue just hard resetting the origin:

git reset --hard origin/develop

Backup is always recommended because you need to copy the modified files to the branch

Arola answered 21/9, 2022 at 11:12 Comment(0)
L
0
find .git/objects/ -size 0 -exec rm -f {} \;
git fetch origin
Liz answered 22/2, 2023 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.