Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git debugging - "error: could not delete references: cannot lock ref" "File exists"

Tags:

git

windows

Different from other answers, I could find no results anywhere for this specific error:

git pull --prune

error: could not delete references: cannot lock ref 'refs/remotes/origin/branchname': Unable to create 'C:/develop/repo/.git/refs/remotes/origin/branchname.lock': File exists.
 
Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue. From github.com/repo

annoyingly, it is trying to cleanup 100s of branches that were deleted on the server... each time I run a pull or fetch, it does the same mass deletion, making no progress :(

like image 296
Kevin Avatar asked Sep 19 '25 00:09

Kevin


1 Answers

Looking in the folder, C:/develop/repo/.git/refs/remotes/origin/ there are no lock files.

There are no other git processes or anti-virus programs running (a typical culprit).

What I ended up doing was search (insensitive, -i) for the branch:

git pull --prune | grep -i branchname

 - [deleted]                 (none)     -> origin/BRANCHNAME
 - [deleted]                 (none)     -> origin/branchname

What I discovered, there was two branches on the server with the same name attempting to be deleted. NEITHER was on my machine.

What this means for Windows systems which have CaSe in-Sensitive file-systems, you cannot have two files with the same name in the directory. Git is trying to create both ".lock' files, but only 1 can exist, so it fails with "File exists" error message.

The error message is misleading... the solution (aside from re-cloning) is to delete all references to the branch refs from your hidden .git folder. Those refs are stored in 3 files in my case:

.git/info/refs

.git/info/refse

.git/packed-refs

I used the SED command to remove the lines in-place

sed -ie '\|branchname\|d' .git/packed-refs .git/info/refse .git/info/refs

--- similar error message which gets more search hits, if you have a local branch (dev-1) with one case, and upstream changes to the other (DEV-1) case ---

error: cannot lock ref 'refs/remotes/origin/DEV-1': 'refs/remotes/origin/DEV-1' exists; cannot create 'refs/remotes/origin/DEV-1'

! [new branch] DEV-1 -> origin/DEV-1 (unable to update local ref)

solution to that was easier, as you can delete the local ref from C:/develop/repo/.git/refs/remotes/origin/ before pulling again.

like image 179
Kevin Avatar answered Sep 20 '25 13:09

Kevin