Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.

Git Error when Creating a Branch

Here is a common Git issue when creating a branch.

$ git checkout -b aaa/bbb

error: unable to create directory for .git/refs/heads/aaa/bbb

fatal: Failed to lock ref for update: No such file or directory

To understand this error message, you need to understand that Git creates a file, in the .git/refs/heads directory, for each branch.

In the example above, we are trying to create a branch called "aaa/bbb". Since this branch has a "/" character in it, Git will first try to create a directory called "aaa" at this location ".git/refs/heads/aaa". Then Git will next try to create a file called "bbb" at this location ".git/refs/heads/aaa/bbb".

The error above occurs when there is already a branch called "aaa", thus there is a file already at this location ".git/refs/heads/aaa". So, when Git tries to create a directory of the same name and location as an existing file, the above error message happens. Therefore, the new branch is not created.

This issue more often happens when creating prototype branches, such as "project" and "project/prototype". Other times this issue happens when nesting branch names, such as "a/b/c/d" and "a/b/c/d/e".

The most obvious solution that I recommend is to never create a Git branch with the "/" character in the name. Instead, I recommend using the "-" character.

This will also have the added benefit of making branch names more easy to read, since some branches have "remotes/origin/" as a prefix if they are on the remote server.

by Phil for Humanity
on 03/26/2015

Related Articles
 » Git Tag
 » Git Checkout Enhancement
 » Git Branches