30

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.

Braiam
  • 1
  • 11
  • 50
  • 74
Tommaso Guerrini
  • 1,160
  • 5
  • 15
  • 32

9 Answers9

29

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
Chuck Le Butt
  • 45,923
  • 58
  • 187
  • 280
cmbuckley
  • 36,905
  • 8
  • 73
  • 90
  • Thank you! Actually I was creating the branch by giving the whole directory once I already was on the branch and not on origin – Tommaso Guerrini Jul 17 '17 at 11:27
  • @cmbuckley If [branch] exists on origin why can't the op just do `git fetch ; git checkout [branch]`? – jacknad Aug 27 '19 at 11:47
  • Yep - the first code block is a simple checkout. The rest of the answer explains the different modes of the checkout command, and how to recover and clean up the local refs. – cmbuckley Aug 27 '19 at 12:47
  • I'm confused. Was the problem with what OP did was that the name of the new branch started with "origin/"? – bob Mar 11 '20 at 18:38
  • The actual problem was probably conflicting branch names (e.g. differing only in case), and an earlier attempt to clean-up manually. There is nothing inherently wrong with naming a branch beginning with "origin/", but it certainly points to a misunderstanding of local and remote branches. – cmbuckley Mar 11 '20 at 21:14
19

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.

Deepak
  • 3,049
  • 1
  • 19
  • 16
19

This worked for me :

git --no-optional-locks fetch --prune origin 
MaTriXy
  • 1,409
  • 1
  • 18
  • 26
9

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.

kiciek
  • 157
  • 2
  • 8
  • This was my issue. Thanks – Chuck Le Butt Nov 05 '21 at 18:42
  • 2
    An actual command that helps someone getting this error is always a good idea in Stack Overflow answers. In this case, for instance, moving the "root" branch to a new name (`git branch -M foo/bar foo/bar/main`) will free up `foo/bar`, and allow it to be created as a dirctory, which leads to the success of `git checkout -b foo/bar/baz`. – ELLIOTTCABLE Apr 05 '22 at 21:59
  • If you have lots of git branches with the same naming pattern, it's possible to batch rename git branches https://stackoverflow.com/a/44570815/3281978 – tgrrr Apr 18 '22 at 05:08
7

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.

Dharman
  • 26,923
  • 21
  • 73
  • 125
Ishan jain
  • 101
  • 1
  • 5
  • I want to create branches in my local with this protocol {username}/featureName but since my username is the same for all branches I want to create , I am unable to create a branch. What should I do? – etotientz Jun 17 '21 at 16:02
3

After trying some of the above solutions with no luck, this worked for me:

git gc --prune=now
General Mac
  • 1,266
  • 1
  • 10
  • 10
  • Worked for me, but would love some more information on what's going on here. For instance: `git-gc - Cleanup unnecessary files and optimize the local repository` from [https://git-scm.com/docs/git-gc](https://git-scm.com/docs/git-gc) – Kent Robin May 31 '22 at 13:50
1

This happend to me, too. The reason was that I tried to create a branch with a too deep directory. The deepest directory to successfully create was: origin/titleA/titleB/nameOfTheNewBranch and I first tried to create it one step deeper in origin/titleA/titleB/titleC/nameOfTheNewBranch which seemed to be too deep 'cause it does not work.

Vanessa
  • 29
  • 1
  • This is likely not because of the *depth*, but because of the *existence of a parent*. Refs (like branches) are files on the filesystem; and if git tries to create a branch named `a/b/c`, it has to create directories named `a` and `a/b`. If you *then* try and create `a/b/c/d`, it's going to fail — `a/b/c` exists, as a file, and cannot be re-created as a dirctory to hold `d` as a file. – ELLIOTTCABLE Apr 05 '22 at 21:57
0

I just go a similar problem and the reason is, because of deleting the branch on the repo I couldn't access it to local so , what I do is delete the local branch and pull the new branch from the repo then create the feature branch and it did work

  • if you delete the branch in the repo it is still available in local
0

refs/heads The HEAD file is usually a symbolic reference to the current branch. The so-called symbolic reference means that it is a pointer to another reference.

every branch you make(such as s/a/b/c/d) will create a path in refs/heads. A file name d which save a SHA-1 values would be touch in the path: refs/heads/s/a/b/c/d

so the problem is, you made a branch named "origin/branch" already, so you can t make any branch named "origin/xxxxx" (or "origin/branch/xxxx") any more