Git Error Cannot Lock Ref: Why Your Pull or Fetch Keeps Failing

Quick Summary

  • The "cannot lock ref" Git error usually means your local refs are out of sync with the remote, and Git can't update them cleanly.
  • It's almost always caused by stale remote-tracking branches, corrupted ref files, or a namespace conflict in your branch names.
  • Two or three commands is usually all it takes to clear it up, no repo cloning required.
a computer screen with a bunch of text on it

Photo by FONG on Unsplash

I was doing a routine git fetch on a project that had been running fine for months. Nothing fancy. And then it just exploded. A wall of red text, half the branches failed to update, and at the end of it all: error: cannot lock ref. I sat there for a second like, what did I even do wrong?

The frustrating part is this error shows up in situations that feel completely innocent. You didn't mess with anything. You just pulled or fetched and now Git is acting like you kicked over its entire filing cabinet. I've seen this hit junior devs and senior engineers equally, usually at the worst possible time, like right before a release or during a hotfix.

The good news is it's almost never serious. Your code is fine. Your repo is fine. It's a ref bookkeeping problem, and once you know what's actually happening under the hood, the fix takes about thirty seconds.

What the Error Actually Looks Like

$ git fetch origin
error: cannot lock ref 'refs/remotes/origin/feature/login': 
  'refs/remotes/origin/feature' exists; cannot create 'refs/remotes/origin/feature/login'
From https://github.com/yourteam/yourrepo
 ! [new branch]      feature/login -> origin/feature/login  (unable to update local ref)

Or sometimes you see it phrased slightly differently:

error: cannot lock ref 'refs/remotes/origin/main': unable to resolve reference 
'refs/remotes/origin/main': reference broken

Both are the same family of problem. Git is trying to write to a ref file and something is in the way, either a conflicting path or a broken/empty file sitting there from a previous interrupted operation.

Why This Happens: The Namespace Conflict

Here's the thing about how Git stores branches locally. Every remote-tracking branch lives as a file inside .git/refs/remotes/origin/. So origin/feature/login is stored at .git/refs/remotes/origin/feature/login.

The problem? If someone on your team previously had a branch just called feature (no slash, no subfolder), Git stored that as a plain file at .git/refs/remotes/origin/feature. Now you're trying to create origin/feature/login, which needs feature to be a directory. But it's already a file. A file and a folder can't share the same name. Git can't do it. So it locks up and throws this error.

This happens a lot on teams where branch naming conventions change over time. Someone had a branch named feature, it got merged and deleted on the remote months ago, but the stale ref is still sitting in your local .git folder doing absolutely nothing useful except breaking your fetches.

Why This Happens: Broken or Empty Ref Files

The second flavor of this error is a corrupted ref. This usually comes from a Git operation that got interrupted mid-write. Power cut, forced kill, disk full, you name it. Git started writing a ref file, didn't finish, and now you have an empty or half-written file sitting there. Every time Git tries to update it, it reads garbage or nothing, can't get a lock on something valid, and bails.

Honestly this one is rarer but it's more annoying because it's not obvious from the error message which file is the problem. You have to go looking.

Green computer code text scrolling on a dark screen during a software installation

Photo by Jake Walker on Unsplash

Fix 1: Prune Your Stale Remote-Tracking Refs

This is the fix that works 80% of the time. Run this:

git remote prune origin

What that does is go through all your locally stored remote-tracking refs and delete the ones where the branch no longer exists on the remote. That stale origin/feature file from six months ago? Gone. Now Git has room to create the feature/ directory it needs.

After that, run your fetch again:

git fetch origin

If you want to combine it into one step going forward, this is the cleaner habit:

git fetch --prune origin

Honestly I'd just alias this or make it your default fetch behavior. Add this to your global git config and you'll hit this error way less often:

git config --global fetch.prune true

Set it once, forget about it.

Fix 2: Manually Delete the Conflicting Ref File

If pruning didn't work, or if Git is saying the ref exists but the remote branch also still exists (so pruning won't touch it), you need to go in manually.

Navigate to your repo and look at the actual file:

# On Mac/Linux
ls .git/refs/remotes/origin/

# On Windows Git Bash
ls .git/refs/remotes/origin/

Find the file that's conflicting with the directory Git is trying to create. Based on the error above, you'd look for a file named feature where Git wants to create a folder named feature.

rm .git/refs/remotes/origin/feature

Then fetch again. That's usually it. The file was just in the way and needed to go.

What I found was that on Windows, this can sometimes get weird because of how the filesystem handles paths. If you're on Windows and rm is giving you trouble in Git Bash, try opening File Explorer, navigate to .git\refs\remotes\origin\, and just delete the file manually. No shame in it.

Fix 3: Fix a Broken Ref File

If your error message says something about "unable to resolve reference" or "reference broken," you're dealing with a corrupted ref. Check if the file is empty:

cat .git/refs/remotes/origin/main

If that prints nothing, the file is empty. That's your problem. You can just delete it and let Git recreate it on the next fetch:

rm .git/refs/remotes/origin/main
git fetch origin

Git will pull the correct ref down from the remote and write it properly. If the branch is still active on the remote, it'll come right back as a clean, valid ref file.

Fallback: Run git fsck and Check for Wider Corruption

If none of the above fixed it, or if you're seeing this across a lot of refs at once, run a quick sanity check on the whole repo:

git fsck --full

This scans your object database for any other corruption beyond just the ref files. Look for lines that say "missing" or "broken" in the output. If you're seeing a lot of those, that's a deeper storage issue, possibly disk-related, and you might want to re-clone the repo from the remote as a clean slate rather than trying to repair in place.

But in my experience, git fsck on a repo that just has a locking error usually comes back clean. It's just the refs that are messed up, not the actual commit objects.

Look, this error sounds scarier than it is. The first time I hit it I thought I'd somehow corrupted the repo and panicked. But 99% of the time it's just an old file sitting in the wrong place. Prune your stale refs, delete what's blocking, fetch again. You'll be back to work in under a minute. And set fetch.prune true globally so your future self doesn't have to deal with this at 2am.


Written by Raho Studio Editorial Team

Practical IT troubleshooting, Zoho Workplace guides, health insights, and sports analysis from people who actually care about getting it right. We research, test, and write so you get real answers — not recycled fluff.

๋Œ“๊ธ€

Glymphatic System and Sleep: What's Actually Happening in Your Brain at Night

Quick Summary Your brain has its own waste-removal system that only activates during deep sleep, and most people are unknowingly sabotaging it every night Understanding the glymphatic system changed how I think about sleep quality versus sleep quantity, and the difference is massive A few simple shifts in sleep position, timing, and evening habits can dramatically improve how well your brain cleans itself overnight Photo by Europeana on Unsplash A few years ago I was waking up foggy every single morning. Not tired exactly, just... cloudy. Like my brain needed an extra hour to actually boot up. I blamed it on stress, on coffee, on screen time. I tried everything. And then I fell down a rabbit hole about what the brain is actually doing while you sleep, and honestly, it changed everything for me. Here's the thing nobody talks about at the doctor's office. Sleep isn't just rest. It's not your body hitting pause. Your brain is running what I can only describe as a full ...

Zoho IMAP Settings: The Complete Fix When Your Email Stops Syncing

Quick Summary Zoho IMAP breaks more often than it should, and the error messages are genuinely useless. The fix is usually a combination of using the right server address, enabling IMAP inside Zoho Mail's settings, and generating an app-specific password. This guide covers the exact steps for Outlook, iPhone, Thunderbird, and Android so you stop going in circles. Photo by Fotis Fotopoulos on Unsplash So your Zoho Mail stopped syncing. Emails aren't coming in, your phone shows a spinning indicator forever, or Outlook throws some vague authentication error and you have no idea what changed. Sound familiar? I've seen this exact situation probably thirty times across different clients, and every single time the person swears they didn't change anything. And honestly? They usually didn't. Zoho changed something on their end, or an app update reset a setting, or someone enabled two-factor authentication without realizing what that does to third-party mail clients. The...

์ด ๋ธ”๋กœ๊ทธ ๊ฒ€์ƒ‰

Low Vitamin D Causes: Why You're Deficient Even If You Go Outside

Quick Summary Most people assume low vitamin D just means they don't get enough sun, but honestly it's way more complicated than that There are hidden causes like gut issues, certain medications, and even your body weight that tank your levels without you realizing This post breaks down what I learned after my own deficiency diagnosis and what actually moved the needle for me Photo by Nicolas Solerieu on Unsplash A couple of years ago I got a routine blood panel done and my doctor called me back almost immediately. My vitamin D was at 18 ng/mL. She said anything under 20 is considered deficient and anything under 30 is insufficient. I was shocked. I live in a sunny state. I walk outside every single day. I'm not exactly a vampire. So I went down a pretty deep rabbit hole trying to figure out why. And what I found was genuinely surprising. It wasn't just about sunlight at all. There were like six different things working against me simultaneously, and I had no idea. ...
↑