How can I fix a missing blob in Git?
I got a "missing blob" after trying to fix "object file is empty" error (actually I ended up with object file .git/objects/f7/880aa1d1a76bfff73f3d602e15b4bc829d6a07
removed from the file system).
In order to solve the issue I followed these steps:
Use the Bash script found here in order to detect a commit containing this missing blob. Put it in the repository's root directory with the
find.sh
name:#!/bin/shobj_name="$1"shiftgit log "$@" --pretty=format:'%T %h %s' \| while read tree commit subject ; do if git ls-tree -r $tree | grep -q "$obj_name" ; then echo $commit "$subject" fidone
And then run it, passing the SHA-1 hash of the missing blob as the argument:
./find.sh f7880aa1d1a76bfff73f3d602e15b4bc829d6a07629afc4 ESLint warning in layers' configuration file is fixed.
629afc4
is part of the commit's SHA-1 (it was a last commit I tried to push to remote repository).Find a file associated with this blob:
git ls-tree -r 629afc4 | grep f7880aa1d1a76bfff73f3d602e15b4bc829d6a07100644 blob f7880aa1d1a76bfff73f3d602e15b4bc829d6a07 src/config/layers.js
It's
src/config/layers.js
in my case.Check whether hash of the file matches the hash in Git tree:
git hash-object src/config/layers.jsf7880aa1d1a76bfff73f3d602e15b4bc829d6a07
If so then we can write file contents to the blob:
git hash-object -w src/config/layers.js
Doing these steps helped me to remove the error and fix a broken local repository. The solution is found in this article.
The accepted answer helped me to fix the problem.
However, I suggest a faster fix if missing blobs are files that are in current directory (that was my case).
This means that, for a reason, a file has not been correctly indexed by git, and is causing the missing blob
.
To find a missing blob of 04da887681cecfd6cd59a928008b0e52ffe2a2bf
, you can go to the .git
directory, and launch :
find . -type f -print -exec git hash-object {} \; | grep -B1 04da887681cecfd6cd59a928008b0e52ffe2a2bf
This will go through your data to find the file that is not indexed.If it find something, you now have the file to index:
./myfile.php04da887681cecfd6cd59a928008b0e52ffe2a2bf
Then, you can index it with: git hash-object -w ./myfile.php
If it doesn't find the file, this means that it was perhaps a previous version of the file, or a file that has been lost.
The accepted answer or any of the variant did not work for me as git fsck
did not show the missing blob neither did
$ git ls-tree -r HEAD | grep <missing blob hash id>
return anything.
What worked for me was a little hack I employed. I am sharing it in case someone comes across it.
I cloned the repo in a new location and checked it out to the branch I was working on. I shelved the changes in the current corrupted repo (as I had a few changes I could not afford to lose) and then copied over the .git
folder from the newly cloned repo to the old repo. Then I ran git pull
which then worked.