Problem:
- accidentally did a git --amend and pushed it to a usb key from first computer
- pulled from the usb key to a second computer
- second computer repository is now corrupted
- git pull to first computer results in a merge conflict; confused about whether this would be a corrupted state as well (if the --amend is corrupt)
Symptoms:
most commands:
fatal: your current branch appears to be broken
.git/refs/heads/master:
$ cat .git/refs/heads/master
file conents of .git/refs/heads/master:
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
$ git status
new file: ...
new file: ... [for every file in the repository; expected since on a corrupted branch]
I'm not sure I am willing to blame git --amend since it seems somewhat benign; maybe something else happened.
How to fix?:
Is fixing this as simple as taking the hash from the latest good commit of .git/logs/refs/heads/master and inserting it manually into .git/refs/heads/master ? How would I do this, if
If so, should I destroy the offending commit (so it doesn't corrupt things later on, with say a git-repack
or something)?
When I tried to less .git/logs/refs/heads/master
and took the [edit:typo] first hash from the last line of the form...
...
[hash for HEAD~2] [hash for HEAD~1] [authorname] ...
[hash for HEAD~1] [hash for HEAD] [authorname] ...
^^^^^^^^^^^^^^^^^ (corrupted)
EOF
... and then pasted into the .git/refs/heads/master file, I am now stuck with...
$ git fsck
error: inflate: data stream error (unknown compression method)
error: unable to unpack header of .git/objects/8f/1da374ffac3711f8cdde57379f90cb03bbb9ea
error: 8f1da374ffac3711f8cdde57379f90cb03bbb9ea: object corrupt or missing: .git/objects/8f/1da374ffac3711f8cdde57379f90cb03bbb9ea
error: inflate: data stream error (unknown compression method)
error: unable to unpack header of .git/objects/ac/2fcd052804fb7adac465220da5bcb04d008fc7
error: ac2fcd052804fb7adac465220da5bcb04d008fc7: object corrupt or missing: .git/objects/ac/2fcd052804fb7adac465220da5bcb04d008fc7
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1147/1147), done.
error: inflate: data stream error (unknown compression method)
error: unable to unpack 8f1da374ffac3711f8cdde57379f90cb03bbb9ea header
error: inflate: data stream error (unknown compression method)
error: unable to unpack 8f1da374ffac3711f8cdde57379f90cb03bbb9ea header
fatal: loose object 8f1da374ffac3711f8cdde57379f90cb03bbb9ea (stored in .git/objects/8f/1da374ffac3711f8cdde57379f90cb03bbb9ea) is corrupt
I could try destroying the loose object, but I'm not sure if it is, in turn, a pointer to (e.g. a tree) of more corrupt objects that must be destroyed as well. I could certainly try to destroy this object if I knew the commend (or could try to rm
it on a backup); should I try that?
Furthermore, how would I repair the USB key and other repo from this mess? Thanks.
(potentially useful references for newbies, unlikely to help people capable of answering this question: https://aboullaite.me/deep-dive-into-git-git-refs/ ) (similar question fatal: your current branch appears to be broken -- does not specify cause of error; many things may cause this error)
edit:
I did rm .git/objects/...
for every object listed above, and now I have...
$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1147/1147), done.
error: refs/remotes/origin/HEAD: invalid sha1 pointer 0000000000000000000000000000000000000000
error: refs/remotes/origin/master: invalid sha1 pointer 0000000000000000000000000000000000000000
error: HEAD: invalid reflog entry 8f1da374ffac3711f8cdde57379f90cb03bbb9ea
error: refs/heads/master: invalid reflog entry 8f1da374ffac3711f8cdde57379f90cb03bbb9ea
error: bad ref for .git/logs/refs/remotes/origin/HEAD
error: bad ref for .git/logs/refs/remotes/origin/master
error: ac2fcd052804fb7adac465220da5bcb04d008fc7: invalid sha1 pointer in cache-tree
broken link from tree b0d598ef5427d59ed31eb1b315c761fc89af40b7
to tree ac2fcd052804fb7adac465220da5bcb04d008fc7
dangling blob f4e39c36cc8df3f9f324c0ccca4ed6a7a3ffe6ac
dangling tree 068716abcf815b4eaf8f0fe74c3020bf6251bba0
dangling blob fb4cfe7c94e8b4d800fdb4935806577b2b99fd94
dangling blob 35cf2ca2ed03811c14f1598c50daacfab9032b8f
missing tree ac2fcd052804fb7adac465220da5bcb04d008fc7
dangling blob d056e38af637cf0de76dac5689a8c5e735d75793
dangling blob 3b3903cc7b4eb035e9c4508024acc3f81c015741
dangling blob b09c3cc95935a327ecf7fad8374f14c4e320f67e
Git 2.40 (Q1 2023) should be more robust in a similar scenario (interrupted pull).
First, don't forget to activate
core.fsync=reference
, as I mentioned here (Git 2.36+).Git 2.40 fixes the sequence to fsync
$GIT_DIR/packed-refs
file that forgot to flush its output to the disk.See commit ce54672 (20 Dec 2022) by Patrick Steinhardt (
pks-t
).(Merged by Junio C Hamano --
gitster
-- in commit 3ed91c5, 02 Jan 2023)