mikdiet
mikdiet

Reputation: 10018

Max length of git branch name

I want to understand what is maximum allowed size of a git branch name.

I am trying to create some long names (as an experiment) so a name with 370 characters was valid, but 380 characters gave me fatal: Failed to lock ref for update: File name too long

So the questions are: what is the maximum number of characters that can be in git branch name? Does it depend on the system? Is it possible to create long branch name in a repository that will be rejected on remote repository (i.e. Github)?

Upvotes: 43

Views: 39295

Answers (3)

dimo414
dimo414

Reputation: 48824

As mentioned in the accepted answer the practical limit is going to be dependent on components other than git such as the filesystem and remote hosts. GitHub appears to reject all branch names longer than 244 bytes. Other git hosts might have different limits, but in practice branch names larger than what GitHub would accept will be a problem for most usages (i.e. it would prevent migrating to GitHub in the future, even if you used a different host today).

Locally (on Ubuntu with ext4) I hit a filesystem error at 251 bytes, because git can't create a [branchname].lock file (as this name exceeds 255 bytes):

$ git branch $(printf 'n%.0s' {1..251})
fatal: cannot lock ref 'refs/heads/nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn': Unable to create '/tmp/foo/.git/refs/heads/nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn.lock': File name too long

However git itself seems happy with far longer branch names, as long as you work around filesystem length limits by including /es in the branch name:

$ git branch $(printf 'm%.0s' {1..250})/$(printf 'm%.0s' {1..250})
# success

So if you're creating branches (on ext4) that you never intend to publish you can make them just about as long as you'd like as long as you structure them like a directory hierarchy.

Upvotes: 3

Issam Ressani
Issam Ressani

Reputation: 213

Open the Github Powershell, and execute the following command :

git config --system core.longpaths true

Upvotes: -3

Wander Nauta
Wander Nauta

Reputation: 19625

The 'File name too long' is indeed an error coming from your system. It is not a restriction in Git. I don't think there's a way of testing the maximum branch name lengths of other systems, like GitHub's, without experimenting - although I don't see why you would want to know...

.

Source: the error mentioned is thrown on line 291 of branch.c. "File name too long" is the standard description for the ENAMETOOLONG error.

Upvotes: 19

Related Questions