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 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.
- 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 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.
git fsck
? – Manvell