问题
I migrated a repository both locally and the remote, and now I am unable to push (or do much of anything) because of a bad tree object,
> git push
error: Could not read 4218a5a380d8b6cbc7f461c22cb48ed66a92c850
fatal: bad tree object 4218a5a380d8b6cbc7f461c22cb48ed66a92c850
fatal: The remote end hung up unexpectedly
4218a5a380d8b6cbc7f461c22cb48ed66a92c850
doesn't exist on my filesystem,
> dir 4218a5a380d8b6cbc7f461c22cb48ed66a92c850 /s /p
Volume in drive C is OS
Volume Serial Number is xxxxxxxx
File Not Found
I have all my source code files. Is my only option now to nuke the git repo and start the history over again?
回答1:
The "nuke and pave" option is not necessarily your only option, but it may be your best one. A "bad tree object" is an unusual error and suggests that parts of your repository may be corrupted (by disk failure, or rogue process / malware / whatever overwriting your files, or attempting to store a Git repository in a dropbox folder, or who knows what). If you are going to nuke it, you might want to back it up first just in case, though.
If you're doing a git push
with no options, that suggests that you have a separate copy of the repository (slightly out of date, perhaps) with most of the code in it, probably on a different machine. That separate copy elsewhere may still be good. You could check that, perhaps by cloning it to a fresh location, and then update the fresh clone with new commits made from whatever you can salvage from the damaged repository, and push those so that the new commits are added to that other repository on the other machine. Then you can really be comfortable with nuke-and-pave since you have two good copies: one in your fresh clone (the third Git in this scenario), and one on the other machine (the second Git here—the first Git is your corrupted repository).
It's also worth doing some background investigation into what corrupted the repository in the first place, since that might still be going on.
As an aside, even if the tree object 4218a5a380d8b6cbc7f461c22cb48ed66a92c850
exists as a loose object, it won't be in a file named 4218a5a380d8b6cbc7f461c22cb48ed66a92c850
. It will be in a directory named 42
stored as a file named 18a5a380d8b6cbc7f461c22cb48ed66a92c850
.
If the object has been packed, it won't be around as a separate file at all, but rather as a packed object in some pack file (whose name is not predictable).
回答2:
There is a commit in git reflog --all that points to 4218a5a380d8b6cbc7f461c22cb48ed66a92c850, that commit is a merge into origin/master
if you have the object remotely, and you have some local work which you don't want to lose by discarding your local repository, you can git clone
to a temporary directory TMP, and then copy all files from TMP/.git/objects/pack
into .git/objects/pack
subdirectory in your folder. Then run git fsck
again to see if it helped and how many more issues there are.
来源:https://stackoverflow.com/questions/48758490/git-recover-from-bad-tree-object