Twice now, I have run into some odd corruption with a local Git repository. Since I had to look it up twice, a quick post is in order.
Symptom
When doing a fetch
or pull
, I would get an error like this:
error: cannot lock ref 'refs/remotes/origin/some/branch-name': is at <hash> but expected <different hash>
I honestly haven’t the slightest idea what is causing it, but it has happened twice now.
The Fix
A Google search led to a few issues in various places, but the one that worked was from the Github desktop repository:
git gc --prune=now
rm -Rf .git/refs/remote/origin
git fetch
I never attempted this with staged or modified files, so, caveat emptor. But my local branches were just fine after, so make I would make sure you do not have any modified or staged files before trying this.