How to deal with Git "Could not read" error
Asked Answered
R

10

60

I am getting this error in my git repository:

22:09:15 $ git status
# On branch master
error: Could not read 8124cc15c63be92d534e4cdfa33c38d54deee122
error: unable to read tree object HEAD
nothing to commit (working directory clean)

A Google search for error: unable to read tree object HEAD doesn't result in much help, this error seems to be very rare. I am not sure how to deal with it. Could it be a hard drive failure?

Edit: The output of git fsck is the following:

broken link from  commit 607328dc80e4901a55b95c683d4fbf43e6df28bf
              to    tree 8124cc15c63be92d534e4cdfa33c38d54deee122
missing tree 8124cc15c63be92d534e4cdfa33c38d54deee122
dangling tree 56b5d4a5e429d251582ec927bca7ef1225510c41
dangling tree 0259d2d38b18b6136bb6070fb41faf3624453cc6
Rahmann answered 2/10, 2009 at 2:13 Comment(1)
It sound like a corruption of some kind. Did you try git fsck?Manvell
A
34

On a "broken link" message, you could follow the GitFaq recommendations:

  • back up all your state so that anything you do is re-doable if you corrupt things more!
  • explode any corrupt pack-files
    • See "man git-unpack-objects", and in particular the "-r" flag.
      Also, please realize that it only unpacks objects that aren't already available, so you need to move the pack-file away from its normal location first (otherwise git-unpack-objects will find all objects that are in the pack-file in the pack-file itself, and not unpack anything at all)
  • replace any broken and/or missing objects
    • This is the challenging part.
      Sometimes (hopefully often!) you can find the missing objects in other copies of the repositories.
      At other times, you may need to try to find the data some other way (for example, maybe your checked-out copy contains the file content that when hashed will be the missing object?).
  • make sure everything is happy with "git fsck --full"
  • repack everything to get back to an efficient state again

Notes:

Update July 2016 (7 years laters), with Git 2.10 soon to be released, you now have:

git fsck --name-objects

It helps naming the origin of those broken links

See "How to fix git error broken link from tree to tree?" for more.

Arruda answered 2/10, 2009 at 6:1 Comment(0)
N
24

I had a similar issue just now. The corruption arose when my laptop did a hard power-off during a git pull. I have a remote backup repository. First I had several object files in .git/objects/??/* that were zero size. After a cp -a backup of the repository, I did this:

  • remove the zero length objects
  • clone the remote repository into a ../fresh/ repository
  • in the broken repository, I did

    cat ../fresh/.git/objects/pack/pack-*.pack | git unpack-objects

This filled up the missing objects in the object database. The repository seems to be back up now.

Nembutal answered 11/9, 2013 at 10:0 Comment(3)
Weird, Git complained that one of my packs was corrupt. I just unpacked that corrupt pack file using this command, it didn't complain during unpacking about any corruption, and now git fsck is happy again!Interpreter
This outputs binary data like PACK^@^@^@^B^@^@^@, sould this be saved somewhere?Dislimn
@ElanRuusamäe Really? It should not output pack data. Did you forget the | git unpack-objects pipe part?Nembutal
P
7

I had the same problem. After a lot of hair-pulling, I discovered that it was cause by changed permission to the repository's git files. I have solved it as follows:

$ cd .git
$ chmod 755 *

Done!

Pleura answered 22/10, 2010 at 19:15 Comment(1)
That was the cause in my case, but although your suggestion would have prevented it happening, it's no use now the problem has already happened - my repository is broken. I fixed it by renaming the project, re-cloning from elsewhere and recreating the couple of commits that I had lost by copying the files from the renamed project. I'm using an older Ubuntu LTS, so it's only got git 1.5.4.3.Inopportune
M
5

I got a similar error in my Homebrew install’s Git repository. Rather than restoring all the missing objects one by one, I found it easier to simply delete the .git directory and create it again by re-cloning from Homebrew’s public repository. These were my steps:

  1. Check what information you have in your Git repository that you won’t get by just re-cloning. For me, it was private branches, stashes, and remotes.
    • Convert stashes into real commits by creating a new branch, applying the stash, and committing with something like “[WIP]” in the name to show that it’s a stash.
    • Save branches that are not on the public remote by pushing them to a remote of your own. This could be a fork of the repository on GitHub, or just a new Git repository in a different location on your machine.
    • If you have more than one remote, save the output of git remote -v, which contains the names and URLs of your remotes, so you can manually add them back later.
  2. Delete your repoistory’s .git directory (or rename it to .git-broken and delete it later). On the command-line, this is rm -rf .git.
  3. Re-clone the remote directory with git clone https://github.com/Homebrew/homebrew.git or whatever URI.
  4. This will have created a new sub-folder homebrew named after the repository. You want just the .git directory from that; your local files are already okay. So mv homebrew/.git .git, and then delete the homebrew folder.
  5. Your Git repository should have no errors, since you recreated it from scratch. Now just restore any information you saved in the first step.
    • If you had additional remotes, add them again with git remote add <name> <url>.
    • If you backed up any branches (or stashes converted to branches) to a remote repository, pull them from that repository to your local repository.
    • If you want, you can convert the stash-branches back to stashes by rolling back the “[WIP]” commit with git reset HEAD^ and saving the working directory to a stash again with git stash save <custom-message>.

If you run git fsck, you should see no errors:

$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (197135/197135), done.
Checking connectivity: 197162, done.
$

And git stash list, git branch, and git remote -v should show the same output as before.

Moneymaker answered 12/9, 2014 at 1:59 Comment(0)
A
1

I solved this by removing the remote and re-adding it.

I had used git fsck --name-objects and it showed that multiple branches were corrupt. Deleting the local and remote copies of those branches did not solve the issue.

However, what solved it was:

git remote remove origin

and then:

git origin add origin <my-origin>

After I did that everything was working again.

Assignor answered 17/1, 2023 at 15:11 Comment(0)
I
0

If you don't have uncommited changes the easiest solution is to delete the local branch: git branch -D [branch name]

and then checkout again the remote branch: git checkout -b [branch name] origin/[branch name]

Idel answered 29/9, 2013 at 8:59 Comment(0)
B
0

i fixed the error by making changes in the same directory/project folder and then tried to commit the new change , what happened is i got an error msg 'invalid object 100644 e38e910ceb18b09f436f353c3a131bfe2caba130 for 'Book/alise_mathe/app/src/main/res/menu/drawermenu.xml' this msg solved the problem , i just refactored the drawermenu.xml by changing the file name to 'drawer_menu.xml'. added the changes committed pushed and thats all. (android-studio)

I hope this helps some how

Bearwood answered 15/7, 2016 at 22:35 Comment(0)
C
0

I fixed this error by deleting the Capistrano 'repo' folder from my remote server directory. I went through a number of the other suggested problems and resolved that the problem didn't lie with my local project. The problem appeared to occur when Capistrano was executing pulling from the repo to the remote. For me, this was perhaps due to a halted deployment that left corrupted objects / object references. My host also had just done a server migration, perhaps something became corrupted during this process.

Contraposition answered 30/10, 2016 at 14:27 Comment(0)
A
0

If none of the above steps work and you have local changes that you want to keep and have a remote repository in tact, this reset script works wonders.

If in addition you need to keep your stash, copy the following files over manually:

  • .git/objects
  • .git/refs/stash
  • .git/logs/refs/stash
Azaria answered 16/9, 2020 at 7:36 Comment(0)
U
0

My usual solution to any missing ref or server did not send all objects error message.

current repo folder called "original"

  1. create a new folder - hereafter called "new"

  2. in "new" run git init

  3. copy from original\\.git\config to new\\.git\config

    (this is a quick/easy way to add all the remotes to the new one)

  4. in "new" run git fetch --all --tags

(get literally every object from the server)

  1. move the folder new\\.git\objects into original\\.git
  2. let the OS merge the folders, if you get a replace confirmations say no (very rare)
  3. in "original" run git gc (remove duplicate objects and ones you don't actually need)
  4. delete "new"
Uncircumcision answered 28/3, 2023 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.