Skip to content

Instantly share code, notes, and snippets.

@rhamaa
Created December 28, 2021 06:23
Show Gist options
  • Save rhamaa/c3ca29db9979d375fe3c1c8df00fb158 to your computer and use it in GitHub Desktop.
Save rhamaa/c3ca29db9979d375fe3c1c8df00fb158 to your computer and use it in GitHub Desktop.
fatal: loose object X (stored in .git/objects/82/X) is corrupt
#! /bin/sh
# TAKEN FROM stackoverflow, but i forgot its source
# Save Git data
cp -r .git gitold
# Remove all empty Git object files
find .git -type f -empty -delete -print
# Get the current branch name
branchname=$(git branch --show-current)
# Get the latest commit hash
commit=$(tail -2 .git/logs/refs/heads/$branchname | awk '{ print $2 }' | tr -d '[:space:]')
# Set HEAD to this latest commit
git update-ref HEAD $commit
# Pull the latest changes on the current branch (considering remote is origin)
git pull origin $branchname
echo "If everything looks fine you remove the git backup running :\n\
$ rm -rf gitold \n\
Otherwise restore it with: \n\
$ rm -rf .git; mv gitold .git"
# another source : https://dev.to/dieunelson/how-to-fix-git-error-object-file-is-empty-49bp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment