Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git fetch gives "cannot lock ref" error message but seems to work anyway

Tags:

git

bitbucket

Sometimes when I try and run git fetch I get the following response:

error: cannot lock ref 'refs/remotes/origin/branchname': is at XXXXXX but expected YYYYYY

The steps that I took that caused this were, to the best of my recollection, the following:

  1. I did a git push from my work-PC, as I usually do, to the origin hosted at Bitbucket;
  2. I did an uncommon git fetch when I got home. There were no errors or warnings;
  3. I did an uncommon git push after working at home. There were no errors or warnings;
  4. I did a git fetch when I arrived at the work today.

Step 4 caused the error message. My local repository had the offending branch checked out. Still I just ran git pull and it seemed to work.

This is not the first time I saw this error message. The first time it happened, I fetched another team's repository to update my local copy which had been sitting untouched (no fetching, no work, no pushing) for one month or two. When I pushed my changes, my co-worker got the error.

So in the two cases it seems to be somewhat related to local repositories which stay untouched for a while. But a sample of two is quite small to draw any conclusions.

I'd like to know what could be causing this. And, more importantly, if it is indeed harmless or if I'm at risk of losing work.

like image 245
giusti Avatar asked Sep 28 '17 12:09

giusti


2 Answers

Following @bernard-paulus's comment, you can try

git gc --prune=now

which worked for me

You can also try

git for-each-ref 
like image 199
David Norman Avatar answered Nov 19 '22 06:11

David Norman


I have tried multiple options but below command worked for me. Putting it here for others if the above command does not work.

git pull -p

like image 1
ManojP Avatar answered Nov 19 '22 06:11

ManojP