How to deal with Git “Could not read” error

前端 未结 8 1152
萌比男神i
萌比男神i 2020-11-30 03:58

I am getting this error in my git repository:

22:09:15 $ git status
# On branch master
error: Could not read 8124cc15c63be92d534e4cdfa33c38d54deee122
error:          


        
相关标签:
8条回答
  • 2020-11-30 04:25

    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!

    0 讨论(0)
  • 2020-11-30 04:25

    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:

    • 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 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.
    • 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.
    • Re-clone the remote directory with git clone https://github.com/Homebrew/homebrew.git or whatever URI.
    • 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.
    • 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 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.

    0 讨论(0)
提交回复
热议问题