How to resolve Git reporting invalid sha1 pointer 0000000000000000000000000000000000000000?

15.2k views Asked by At

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

4

There are 4 answers

1
Drew Noakes On

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.

0
anion On

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.

0
Chung Ho Duc On

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.

0
TvC On

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