Skip to content

Instantly share code, notes, and snippets.

@azedo
Last active May 6, 2025 10:48
Show Gist options
  • Save azedo/1c8eae468e54e225334590c0c7a46b0b to your computer and use it in GitHub Desktop.
Save azedo/1c8eae468e54e225334590c0c7a46b0b to your computer and use it in GitHub Desktop.
How to recover dangling blobs from git
# 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"
@azedo
Copy link
Author

azedo commented May 6, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment