GitHub Error error: could not delete reference refs/remotes/origin/HEAD: cannot lock ref 'refs/remotes/origin/HEAD': unable to resolve reference 'refs/remotes/origin/HEAD': reference broken
and git gc
Command
Recently, when I pushed commits to GitHub via GitHub Desktop, I encountered the following error every time although it didn’t affect pushing normally (the error never occurred before):
1
error: could not delete reference refs/remotes/origin/HEAD: cannot lock ref 'refs/remotes/origin/HEAD': unable to resolve reference 'refs/remotes/origin/HEAD': reference broken
About this problem, GitHub user “zhzoo” provides a way to solve it1:
running: git gc --prune=now
and deleting .git/refs/remote/origin
where the git gc
command is to “cleanup unnecessary files and optimize the local repository”2:
Runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase performance), removing unreachable objects which may have been created from prior invocations of git add, packing refs, pruning reflog, rerere metadata or stale working trees. May also update ancillary indexes such as the commit-graph.
and the --prune=<date>
option2:
--prune=<date>
Prune loose objects older than date (default is 2 weeks ago, overridable by the config variable gc.pruneExpire
). --prune=now
prunes loose objects regardless of their age and increases the risk of corruption if another process is writing to the repository concurrently. --prune
is on by default.
But when I executed git gc --prune=now
, there were other errors:
1
2
3
4
5
$ git gc --prune=now
error: bad ref for .git/logs/refs/remotes/origin/HEAD
fatal: bad object refs/remotes/origin/HEAD
fatal: failed to run repack
To resolve it, an extra command mv .git/refs/remotes/origin/HEAD /tmp
should be executed firstly3.
And at this time, it’s okay to run the command git gc --prune=now
again:
1
2
3
4
5
6
7
8
$ git gc --prune=now
Enumerating objects: 3385, done.
Counting objects: 100% (3385/3385), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3363/3363), done.
Writing objects: 100% (3385/3385), done.
Total 3385 (delta 1896), reused 0 (delta 0), pack-reused 0
At last, delete .git/refs/remote/origin
, and the problem is successfully solved.
References