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

2

11

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.

Ballroom answered 28/9, 2017 at 12:32 Comment(2)
git gc --prune=now worked for me (see #45948781 )Pebbly
git pull -p worked for me.Pyrogen
P
28

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

git gc --prune=now

which worked for me

You can also try

git for-each-ref 
Pneumatophore answered 26/3, 2018 at 11:11 Comment(2)
can you explain what is the issue & how it got solved using this command. I googled it, but didn't able to understandDrambuie
Neither of these commands worked: I still get the exact same error message.Pungent
R
2

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

Reposition answered 12/1, 2022 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.