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.
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.
Photo by David Pupฤzฤ on Unsplash
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.
Photo by Ferenc Almasi on Unsplash
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.
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.
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.
๋๊ธ
๋๊ธ ์ฐ๊ธฐ