34

A co-worker and I have been working on the same branch for a week, constantly pushing/pulling changes and all of a sudden today, I hit 'pull' to see if there were any changes I needed to pull and I got an error.

This is in sourcetree by the way. The error was this:

git -c diff.mnemonicprefix=false -c core.quotepath=false fetch origin
error: cannot lock ref 'refs/remotes/origin/angular_removal': unable to resolve reference 'refs/remotes/origin/angular_removal': reference broken
From https://bitbucket.org/colossus
 ! [new branch]        angular_removal -> origin/angular_removal  (unable to update local ref)

I'm in sourcetree, which has a terminal built in, but I can't seem to find a resolution here.

msanford
  • 11,803
  • 11
  • 66
  • 93
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • Open that terminal and type `git for-each-ref`; do you get a `fatal: missing object 0000000000000000000000000000000000000000 for refs/remotes/origin/angular_removal`? – msanford Nov 10 '17 at 15:30
  • All I get for angular_removal is this: ```warning: ignoring broken ref refs/remotes/origin/angular_removal a7fc93bf2e87e7e176f286449a332cb881ea40c5 commit refs/heads/angular_removal ``` – Geoff_S Nov 10 '17 at 15:32
  • Try this https://gist.github.com/libitte/cbde168d26bc5faf9bf9fef648091b42 and then this https://stackoverflow.com/questions/23749886/git-unable-to-resolve-references-when-pushing – msanford Nov 10 '17 at 15:41
  • Hmm still no luck. Even when I prune I get the error there – Geoff_S Nov 10 '17 at 15:55
  • @msanford if I run ```git gc --prune=now``` I get this: ```error: bad ref for .git/logs/refs/remotes/origin/angular_removal fatal: bad object refs/remotes/origin/angular_removal error: failed to run repack ``` – Geoff_S Nov 10 '17 at 15:58
  • What does `git fsck --full` say? – msanford Nov 10 '17 at 16:00
  • ```Checking object directories: 100% (256/256), done. Checking objects: 100% (35963/35963), done. error: refs/remotes/origin/angular_removal: invalid sha1 pointer 0000000000000000000000000000000000000000 error: bad ref for .git/logs/refs/remotes/origin/angular_removal dangling commit 27d19ab92533410dc72eedba5ee86ac3423902a3 ``` – Geoff_S Nov 10 '17 at 16:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158712/discussion-between-msanford-and-tom-n). – msanford Nov 10 '17 at 16:06

13 Answers13

53

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.**

Raja Anbazhagan
  • 4,092
  • 1
  • 44
  • 64
  • 3
    This is the real answer. Performing the steps and a fetch afterwards I have got it working – Kay Sep 13 '18 at 07:42
  • This solution worked for me. I was getting the exact error as OP and this solution worked beautifully. Though I performed an extra step: After following the above steps **I deleted the entry corresponding to the faulty branch** (for OP it's origin/angular_removal) from `.git\FETCH_HEAD` and then fetch worked. Not sure if the extra step was necessary though... – Himanshu Jan 04 '19 at 11:40
  • 1
    `FETCH_HEAD` is a kind of a tracker file to inform git which branch is fetched till which commit... This file will get refreshed automatically on `git fetch`. Ideally it's better if this file is untouched. – Raja Anbazhagan Jan 07 '19 at 04:55
  • it worked at first and then when update the project second time, I got the same error – sia Sep 24 '20 at 21:50
50

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.
Dudar
  • 719
  • 6
  • 8
17

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

Lucas Castro
  • 301
  • 3
  • 5
4

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.

Bryan Norden
  • 2,397
  • 18
  • 22
3

Multiple solutions didn't quite resolve it but I ended up re-cloning

Geoff_S
  • 4,917
  • 7
  • 43
  • 133
3

Just happened to me as well; one of my ref branches under my alias was corrupted. Fix:

rm <root of repository>.git/refs/remotes/origin/<your alias>
git fetch

You can use rd /s instead of rm on Windows.

Here are the detailed steps I took to fix the issue. You may choose to skip some of the steps below:

  1. I first committed all my local changes in the local branch that was giving me the issue.
  2. Created git patches for my local branch.
  3. Deleted my local branch.
  4. Applied the following fix
  5. Checked out the remote branch after running git fetch
  6. Applied the necessary patch files to the local branch.

This made git restore the ref file. After that everything worked as expected again.

user3613932
  • 1,219
  • 15
  • 16
3

I had a similar issue, but I couldn't push my commit from my local branch to the remote branch. Looking inside sourcetree under my remote branches, my branch suddenly didn't exist. I tried @Dudar answer, but the branch wasnt mentioned in the packed-refs file. So what I did was:

  1. go into .git\refs\remotes\origin\feature subfolder of my local repository;
  2. delete the file named as my branch causing the issue;
  3. push my changes

Note: My branch existed in a subfolder of origin called "feature".

Adam
  • 93
  • 7
2

Locate to the project folder in Terminal

git gc --prune=now
rm -rf .git/refs/remotes/origin
Paul T.
  • 4,938
  • 7
  • 45
  • 93
1

Update ref.

$ git update-ref -d refs/remotes/origin/[branchname]
Anushree
  • 225
  • 3
  • 4
0
 git pull

resulted in the following error.

error: cannot lock ref 'refs/remotes/origin/e2e/gb-feedback-test': is at 2962f77c6b4248a82efbd4d3c*******f but expected c3c0af7e310247b4e8dfd94883******5
From https://company.com
 ! c3c0af7e3..2962f77c6  branch-name -> origin/branch-name  (unable to update local ref)

It was resolved by

git pull origin master
arun8
  • 1,191
  • 2
  • 14
  • 26
0

Do a

git gc --auto 

and then do

git pull
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
0

TL;DR - Delete the local git folder and clone the repo again

If none of the solutions works, delete the local folder and clone the folder again from the git repo.

Note: This will remove the other branches of the local repo.

Siva-Dev-Wizard
  • 127
  • 1
  • 10
0

Case 1: Let check branchs from git-server if they are duplicate or not.

Example: two branchs below are duplicate:

- upper_with_lower
- UPPER_with_lower

---> Let consider removing one of them.

Case 2: The branch you are pushing are duplicate with other branch.

alphaplus
  • 344
  • 1
  • 3
  • 9