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:

  1. Delete the file .git/packed_refs
  2. Do a pull.
  3. 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 the ref 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:

  1. go into .git subfolder of my local repository;
  2. open packed-refs file;
  3. find the line with branch name from error message;
  4. remove it from this file;
  5. now you can do fetch or pull all you like.