Last active
May 6, 2025 10:48
-
-
Save azedo/1c8eae468e54e225334590c0c7a46b0b to your computer and use it in GitHub Desktop.
How to recover dangling blobs from git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a temporary directory to store lost objects | |
mkdir -p lost-found | |
cd lost-found | |
# Extract all dangling blobs to files | |
git fsck --lost-found --no-reflogs | grep "dangling blob" | \ | |
while read -r line; do | |
blob_hash=$(echo $line | awk '{print $3}') | |
git cat-file -p $blob_hash > blob_$blob_hash | |
done | |
# Now you can search through all the extracted files | |
grep -r "search-term" . | |
# Or use ripgrep for faster searching | |
rg "search-term" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very useful when you accidentally remove a modified file from git history (tracked or untracked).
Make sure to use this as soon as possible since the history will get changed eventually and you will loose it.