Git fatal: cannot lock ref
Solution 1
You shouldn't be checking out branches that begin with "origin" or any other existing branch name.
Assuming that branch exists on origin, you should do the following:
git checkout feature/IF-53-change-validation-window/Tommaso
If you run git branch
I expect you will see local branches with origin
in the name.
The format git checkout X
is shorthand for "look for a local branch X
and check that out if it exists; otherwise look for a remote branch X
and check that out locally (git checkout -b X origin/X
)."
If you are creating a new local branch, you will often do the following:
git checkout -b new-branch
This will create a new branch pointing at the same commit as you had checked out previously.
To fix your current state, you can likely do this (see here):
git update-ref -d refs/heads/origin/branch
Solution 2
Sometimes this can happen if the name of the branch you create doesn't match git naming conventions or those set by you projects git administrator. Changing the name to one that does, can can fix this issue.
Solution 3
This worked for me :
git --no-optional-locks fetch --prune origin
Solution 4
This happened with me too. The reason was, I was checking out a branch named feature
, but in my local I already had a branch called feature/new-feature
.
Solution 5
I was looking for answer here, but actually my problem was simpler, yet unresolvable.
- fresh clone of repo
git checkout foo/bar
git checkout -b foo/bar/baz
- got similiar error message.
As described here, you cannot use foo/bar
both as a branch and directory. I had to change proposed naming convention.
Tommaso Guerrini
Updated on December 18, 2021Comments
-
Tommaso Guerrini about 1 year
I'm trying to create a branch on the current branch on my Ubuntu guest. Unfortunately I keep getting this error:
git checkout -b origin/feature/IF-53-change-validation-window/Tommaso fatal: cannot lock ref 'refs/heads/origin/feature/IF-53-change-validation-window/Tommaso': 'refs/heads/origin/branch' exists; cannot create 'refs/heads/origin/branch/Tommaso'
I tried
git gc --prune=now
as suggested here link, but keep getting the same error.