Is possible to push the non-pushed commits if the git files contain corrupt loose objects?

StackOverflow https://stackoverflow.com/questions/20868411

  •  23-09-2022
  •  | 
  •  

Question

git status outputs the following:

# On branch master
error: corrupt loose object '1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d'
fatal: loose object 1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d (stored in .git/objects/1e/2dc6d8b6eec682f3ecc9dd879445fea5d6a34d) is corrupt

I have 12 non pushed commits. I can see them in git log.

Is there any hope to push them?

git push outputs:

error: corrupt loose object '1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d'
fatal: loose object 1e2dc6d8b6eec682f3ecc9dd879445fea5d6a34d (stored in .git/object/1e/2dc6d8b6eec682f3ecc9dd879445fea5d6a34d) is corrupt
error: failed to push some refs to 'git@bitbucket.com:username/reponame.git'

Before having these corrupted files I run:

find . -type f -print0 | xargs -0 sed -i 's/\s\+$//g'

(in the git repo directory)

This affected the .git files.


Update: I tried the VonC's suggestion running the following commands:

cd ~
$ git clone git@bitbucket.org:username/reponame.git
Cloning into 'reponame'...
remote: Counting objects: 1561, done.
remote: Compressing objects: 100% (853/853), done.
remote: Total 1561 (delta 966), reused 1067 (delta 642)
Receiving objects: 100% (1561/1561), 223.80 KiB | 94.00 KiB/s, done.
Resolving deltas: 100% (966/966), done.
Checking connectivity... done
$ cd reponame/
$ $ git remote add yourLocalRemoteName /home/user/corupted/git/repository/folder
$ git pull yourLocalRemoteName
fatal: attempt to fetch/clone from a shallow repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Was it helpful?

Solution

You could try and:

  • clone your BitBucket repo again

  • add in that new clone a remote referencing your current local clone

      git remote add corrupt/path/to/your/first/cloned/repo
    
  • git pull corrupt, and then git push


That being said, after using git fsck for confirmation, you can try and solve that corrupt object.
As usual, make a backup of that local repo first.


After discussion, it turns out that:

  • the corrupt repo is a shallow one (cloned that way automatically by a tool).
    That means you can clone it easily, even when you do a rm .git/shallow, as suggested here.

  • one way to unshallow a shallow repo is to unpack a fresh repo and copy over the .git/objects/pack folder to the corrupted repo:

      git clone git@bitbucket.org:username/reponame.git repo2
      mkdir packs
      cp repo2/.git/objects/packs/* packs
      cd repos2
      git unpack-objects < ../packs/*.pack
      cd ..
      cp -r repo2/.git/objects/* corrupt/.git/objects/
    
  • That will remove any error due to legacy unpacked object being corrupted by the sed command, but that won't fix the ones done since that clone.

For a corrupted repo, the usual commands are listed in this answer and this comment.
In the end, the OP had:

git fsck --full
error: corrupt loose object '3ddb67eb5343e75ac36656c86c4386f8dd117df4'
fatal: loose object 3ddb67eb5343e75ac36656c86c4386f8dd117df4 (stored in .git/objects/3d/db67eb5343e75ac36656c86c4386f8dd117df4) is corrupt

(no more:

error: inflate: data stream error (incorrect data check)
error: sha1 mismatch ...

which were because of legacy commit corrupted)

For solving that, one can try and follow "Documentation/howto/recover-corrupted-blob-object.txt".


That being said, if that doesn't pan out, it is easier to at least group all last commits into one:

git clone git@bitbucket.org:username/reponame.git repo2
cd repo2
git --work-tree=../corrupt add -A .
git commit -m "last changes"
git push

One can try and rebuild all 12 commits, but that isn't obvious to do, especially when multiple modifications are done within one file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top