How to resolve Git reporting invalid sha1 pointer 0000000000000000000000000000000000000000?
Asked Answered
A

4

23

My computer shut down suddenly and now the Git repository of the project which I worked on is defect. I have Git 2.20.1 installed. I immediately created a backup copy of the project folder to play around with the original. I have local branches which I do not want to loose.

$ git status
error: object file .git/objects/c7/8e67bcad2ea132e3df8f80bb42b4804ab4f2a3 is empty
fatal: loose object c78e67bcad2ea132e3df8f80bb42b4804ab4f2a3 (stored in .git/objects/c7/8e67bcad2ea132e3df8f80bb42b4804ab4f2a3) is corrupt

Shorted output:

$ git fsck --name-objects
error: object file .git/objects/04/f338bf0dc0770b841b291af6cdd4c9130edb84 is empty
error: unable to mmap .git/objects/04/f338bf0dc0770b841b291af6cdd4c9130edb84: No such file or directory
error: 04f338bf0dc0770b841b291af6cdd4c9130edb84: object corrupt or missing: .git/objects/04/f338bf0dc0770b841b291af6cdd4c9130edb84
error: object file .git/objects/2e/07479ec010e5b459387d841a576e1ead97f850 is empty
error: unable to mmap .git/objects/2e/07479ec010e5b459387d841a576e1ead97f850: No such file or directory
error: 2e07479ec010e5b459387d841a576e1ead97f850: object corrupt or missing: .git/objects/2e/07479ec010e5b459387d841a576e1ead97f850
error: object file .git/objects/34/b03409d09382c44f50409e4388850ca9c82c77 is empty
error: unable to mmap .git/objects/34/b03409d09382c44f50409e4388850ca9c82c77: No such file or directory
...
error: object file .git/objects/c7/8e67bcad2ea132e3df8f80bb42b4804ab4f2a3 is empty
error: unable to mmap .git/objects/c7/8e67bcad2ea132e3df8f80bb42b4804ab4f2a3: No such file or directory
error: c78e67bcad2ea132e3df8f80bb42b4804ab4f2a3: object corrupt or missing: .git/objects/c7/8e67bcad2ea132e3df8f80bb42b4804ab4f2a3
error: object file .git/objects/e5/19b2fbf083f68694af97ccddf46bc28c6ee8f7 is empty
error: unable to mmap .git/objects/e5/19b2fbf083f68694af97ccddf46bc28c6ee8f7: No such file or directory
error: e519b2fbf083f68694af97ccddf46bc28c6ee8f7: object corrupt or missing: .git/objects/e5/19b2fbf083f68694af97ccddf46bc28c6ee8f7
error: object file .git/objects/ec/289b8e8d17c47644d5bdbfbfc8b2957d2a430e is empty
error: unable to mmap .git/objects/ec/289b8e8d17c47644d5bdbfbfc8b2957d2a430e: No such file or directory
error: ec289b8e8d17c47644d5bdbfbfc8b2957d2a430e: object corrupt or missing: .git/objects/ec/289b8e8d17c47644d5bdbfbfc8b2957d2a430e
error: object file .git/objects/ed/33778ddc07a26a37c6f200f35ffee12e365ea4 is empty
error: unable to mmap .git/objects/ed/33778ddc07a26a37c6f200f35ffee12e365ea4: No such file or directory
error: ed33778ddc07a26a37c6f200f35ffee12e365ea4: object corrupt or missing: .git/objects/ed/33778ddc07a26a37c6f200f35ffee12e365ea4
error: object file .git/objects/f6/5686bed85025fd7adbf209ba93846b18800d70 is empty
error: unable to mmap .git/objects/f6/5686bed85025fd7adbf209ba93846b18800d70: No such file or directory
error: f65686bed85025fd7adbf209ba93846b18800d70: object corrupt or missing: .git/objects/f6/5686bed85025fd7adbf209ba93846b18800d70
error: object file .git/objects/fd/cdc518b01c670cd38c15903fd3e84554114d38 is empty
error: unable to mmap .git/objects/fd/cdc518b01c670cd38c15903fd3e84554114d38: No such file or directory
error: fdcdc518b01c670cd38c15903fd3e84554114d38: object corrupt or missing: .git/objects/fd/cdc518b01c670cd38c15903fd3e84554114d38

Checking object directories: 100% (256/256), done.
Checking objects: 100% (40855/40855), done.
error: refs/remotes/origin/colors-housekeeping: invalid sha1 pointer 0000000000000000000000000000000000000000
error: bad ref for .git/logs/refs/remotes/origin/colors-housekeeping
error: object file .git/objects/ec/289b8e8d17c47644d5bdbfbfc8b2957d2a430e is empty
fatal: loose object ec289b8e8d17c47644d5bdbfbfc8b2957d2a430e (stored in .git/objects/ec/289b8e8d17c47644d5bdbfbfc8b2957d2a430e) is corrupt

colors-housekeeping is a branch which is already merged to master and already I edited other files afterwards, committed and merged another branch.

What are the steps I can take to fix the corrupt objects?

Related

Allomorphism answered 15/12, 2019 at 19:41 Comment(6)
What happens when you use git reflog --all?Buke
@KarolDowbecki The reflog is output as expected. I cannot see an error output there.Allomorphism
It's difficult to repair a broken repository. Your best bet is usually to use a different clone of the same repository as a new (correct) base, recover what files you can from the broken repository, and put new—different hash ID—commits into the replacement repository. Just use the broken one as a source of individual files. Use git fsck --lost-found to make otherwise-inaccessible "blob" objects accessible via .git/lost-found/. Their file names will generally be impossible to recover in an automated manner.Bleeding
Was your last operation (before the crash) a commit?Ima
@NoufalIbrahim I believe it was a commit - not sure. By now it is a bit time ago. I still have the defect repository lying around.Allomorphism
It's probably my "hands dirty" instincts speaking but the reason I asked is because you might be able to do an ls -ltr on the .git/objects directory to find out the lastest few objects that were touched. Depending on what you put in, there might be a commit, a few blobs and perhaps a few trees. If you can find the commit before that, you might be able to restore the repository to that state using a reset. Then manually take out the objects added since and inspect/repair them.Ima
A
18

This occurred for me when a repo I had cloned renamed its default branch from master to main.

The error was:

error: refs/remotes/origin/HEAD: invalid sha1 pointer 0000000000000000000000000000000000000000

Let's look at that pointer file on disk:

$ cat .git/refs/remotes/origin/HEAD
ref: refs/remotes/origin/master

After editing that file and replacing master with main, I was once again able to fetch/fsck/gc without errors.

Alexine answered 15/6, 2021 at 1:18 Comment(1)
Thank you!! The same thing happened to me when the main branch changed.Illimitable
H
8

error: unable to mmap... is one error, invalid sha1 pointer 000000... is another.

For anyone (like me) who is here who has only the sha1-pointer-error but no mmap-errors:

The sha1-pointer-error might mean that the expected pointer-file does not exist. refs/remotes/origin/colors-housekeeping probably contains content like:
ref: refs/remotes/origin/notexistingbranch
or:
6ca0197d8f15f381d67e3502e678ca3d17dbbea2
but this file (ref) or commit-id can not be found. In both cases you can fix the error by writing any known commit-id or pointer to a valid commit-id in the file. In other words, you reassign the cached value of the commit of the branch origin/colors-housekeeping. When you fetch from origin git will write the correct commit-id of origin/colors-housekeeping in refs/remotes/origin/colors-housekeeping again.

Hae answered 9/6, 2020 at 10:40 Comment(0)
G
3

I had the same problem and this is how I fixed it:

Edit the file: .git/refs/heads/colors-housekeeping Change the commit id (sha1 string) to any valid commit id that you know (should be the newest one that you can find) of that branch you were working on.

Then run the command:

git fetch

then you're fine now.

Garretgarreth answered 21/6, 2022 at 9:6 Comment(0)
C
1

For me I solved it by running the following on the remote's git repo.

git fsck --full 

error: refs/heads/develop-xxx’s MacBook Pro: invalid sha1 pointer 0000000000000000000000000000000000000000

Then I deleted the file at:

.git/refs/heads/HEAD-xxx’s MacBook Pro

In the local git repo I could now perform:

git fetch --all    
git push remotename branchname
Chemar answered 22/3, 2023 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.