Sourcetree/GIT - Cannot lock ref/reference broken, when pulling
NOTE: This is the only solution that seems to work for some people, but is also a dangerous operation, that has broken some people's local repos. Please use carefully, and at your own discretion.
Here are the steps that fixed it for me:
- Delete the file
.git/packed_refs
- Do a pull.
- Run
git pack-refs
to recreate the packed_refs file.
For reference, see: https://git-scm.com/docs/git-pack-refs
I was able to get this fixed by deleted all the items in the .git/packed-refs
file.
Then I just did a git pull in terminal to get all the references/code from the remote.
The accepted solution provides details only about how OP got around the issue and it is not a definitive answer.
For the sake of people like me who end up here from Google, Here is the solution that actually works.
Lets say, if the error message looks like below,
error: cannot lock ref 'refs/remotes/origin/angular_removal': unable to resolve reference 'refs/remotes/origin/angular_removal': reference broken
Here, the offending element is a corrupted file named refs/remotes/origin/angular_removal
which resides in the .git
hidden folder.
Inorder to fix this, run the following commands under your repository root directory.
rm .git/refs/remotes/origin/angular_removal
git fsck
The first command alone should fix the issue as git
tries to reinitialize the missing references.
The command git fsck
is to there just to check if the repository is in good health.
NOTE : The
ref
file would be of different for others. So make sure you are using theref
file name from the error message that you got.**
The error text was slightly different for me like:
unable to update local ref it is [hash_code_1] but expected [hash_code_2]
.
So the command like
rm -rf .git/refs/remotes/origin/angular_removal
helped me only to do fetch once. Then the message would return again.
What actually helped in this situation to fix the issue permanently was:
- go into .git subfolder of my local repository;
- open packed-refs file;
- find the line with branch name from error message;
- remove it from this file;
- now you can do fetch or pull all you like.