Sub topic 5
Naming branches
This chapter assumes you can create and switch branches with git switch -c and that you know a branch is a pointer to a commit. Git accepts most names, but some names are rejected, some cause trouble later, and a good convention saves your team time. The chapter covers all three.
What Git allows
A branch name is stored as a reference named refs/heads/<name>. Git applies its reference-name rules to that path, so the rules apply to your branch names.
A name is rejected if it:
- contains a space,
~,^,:,?,*,[, or\ - contains control characters
- contains
..or@{ - begins or ends with
/, or contains// - ends with
. - has a path component that begins with
.or ends with.lock - is the single character
@ - begins with
- - is
HEAD
You can test a candidate name without creating anything. git check-ref-format --branch prints the name if it is valid. If it is not, the command exits with a non-zero status.
git check-ref-format --branch feature/login-form
git check-ref-format --branch "bad name"
echo "exit status: $?"The first command prints feature/login-form. The second prints nothing, and the echo shows a non-zero status such as 1.
The same function is useful in scripts that create branches from user input, such as a ticket title.
title="Fix: crash on empty cart"
slug=$(printf '%s' "$title" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9' '-' | sed 's/^-//; s/-$//')
git check-ref-format --branch "bugfix/$slug" && git switch -c "bugfix/$slug"Here the title becomes bugfix/fix-crash-on-empty-cart. The tr -cs step collapses every run of characters outside a-z0-9 into a single hyphen.
Slashes make hierarchies
A / in a branch name creates a hierarchy. Git does not treat it as special, but tools do. git branch --list accepts glob patterns, and most graphical clients and web interfaces group branches by prefix.
git switch -c feature/login
git switch -c feature/signup
git switch -c bugfix/cart-total
git branch --list 'feature/*'The last command shows only feature/login and feature/signup.
On the remote, the same names appear under the remote's name: origin/feature/login. The remote name is a prefix, so the full reference is refs/remotes/origin/feature/login.
The directory and file conflict
With Git's default file-based reference storage, each branch is a file under .git/refs/heads/. A name containing slashes becomes a directory path. This means one branch name cannot be a prefix of another, because a path cannot be both a file and a directory.
git switch -c feature
git switch -c feature/loginThe second command fails with an error like this:
fatal: cannot lock ref 'refs/heads/feature/login': 'refs/heads/feature' exists; cannot create 'refs/heads/feature/login'The reverse also fails. If feature/login exists, you cannot create feature. Pick a naming scheme in which every branch has the same shape, such as always type/description. Then a bare feature branch never exists.
If you inherit a repository with a conflicting name, rename the old branch first:
git branch -m feature feature/base
git switch -c feature/loginBranches that exist on a remote can cause the same problem for remote-tracking names. After a fetch, if origin/feature exists and someone pushes feature/login, your fetch reports a similar lock error. The fix is to delete the old branch on the remote or to prune the stale tracking reference with git fetch --prune.
Case, characters, and portability
Branch names are case-sensitive in Git's data model. On a case-insensitive filesystem, which is the default on macOS and Windows, the file-based storage cannot hold Feature/Login and feature/login at the same time. On those systems the two names collide, or one overwrites the other. The same names work on Linux, so a team on mixed systems can hit the problem unexpectedly.
The safe habit is to use lowercase everywhere.
Stay with letters, digits, hyphens, and slashes. These characters are legal but cause friction:
| Character | Legal? | Why to avoid it |
|---|---|---|
| Space | No | Rejected outright |
| `_` | Yes | Fine, but mixing it with `-` makes names harder to type consistently |
| `.` inside a name | Yes | Fine for versions such as `release/1.2`, but not at the start or end of a path component |
| `#` | Yes | The shell treats `#` as a comment when it starts a word |
| Non-ASCII letters | Yes | Terminal, filesystem, and web interface handling varies |
| Leading `-` | No | Would be parsed as a command-line option |
Names that confuse Git
Git resolves a name by trying several places: a branch, a tag, a remote-tracking name, and others. If two kinds of reference share a name, Git warns about an ambiguous reference.
git tag v1.0
git branch v1.0
git log v1.0The last command prints a warning like warning: refname 'v1.0' is ambiguous, and then uses the tag. Do not give a branch the same name as a tag. Also avoid branch names that look like abbreviated commit hashes, such as deadbeef, since Git may interpret the name as a commit.
Do not name a branch after a remote. A branch called origin makes origin ambiguous in commands such as git push origin.
Conventions that teams use
Git does not require a convention, but a consistent one helps people and automation. The most common shape is:
<type>/<ticket-or-scope>-<short-description>| Prefix | Typical use | Example |
|---|---|---|
| `feature/` | New functionality | `feature/PAY-142-apple-pay` |
| `bugfix/` or `fix/` | Defect fixes | `bugfix/cart-total-rounding` |
| `hotfix/` | Urgent production fixes | `hotfix/2.3.1-null-session` |
| `release/` | Release preparation | `release/2.4` |
| `chore/` | Maintenance, dependencies, tooling | `chore/bump-node-20` |
| `user/<name>/` | Personal experiments | `user/ana/spike-cache` |
Some practical points:
- Include the issue-tracker ID when you have one. Many trackers and code-hosting platforms link a branch to its ticket when the ID appears in the name.
- Keep the description short, with hyphens between words. Names appear in terminal output, pull request titles, and CI logs.
- Describe the purpose of the work, not the author's mood.
fix/stuffandtest2tell the reader nothing a month later. - Name
release/branches after the version they prepare. Namehotfix/branches after the version they patch or the defect. - Use prefixes that your automation can match. A CI rule such as "run deployment checks for
release/*" only works if the prefix is used consistently.
Some repositories enforce a convention. A server-side pre-receive hook, or a branch-naming rule on the hosting platform, can reject pushes that do not match. A local reference-transaction hook can also check names, but it only protects the machine where it is installed.
The default branch
The first branch in a new repository is set by the init.defaultBranch setting. Without it, Git uses its built-in default, which is master in older versions of Git and in recent versions unless configured otherwise. Most hosting platforms and tutorials now use main. Set it once so new repositories match your team:
git config --global init.defaultBranch mainYou can also pass -b main to git init for a single repository. To rename an existing default branch, use git branch -m master main, then update the remote and every clone, as later chapters on renaming and remotes describe.
Choosing a name: a quick check
Renaming a badly named branch
A name is not permanent. git branch -m renames a branch and moves its reflog with it:
git branch -m test2 feature/PAY-142-apple-payRenaming changes only the local branch. If you already pushed the old name, push the new one and delete the old remote branch, as the next chapters on remotes explain. Rename before you share a branch whenever you can.
Exercise
A repository already has a branch called release. You run git switch -c release/2.4 and get a "cannot lock ref" error. Why does it fail, and what is a naming scheme that avoids the problem?
Show answer
With file-based reference storage, refs/heads/release is a file, and release/2.4 would need release to be a directory. A name cannot be both, so Git refuses. Rename the existing branch (for example git branch -m release release/base) or choose a scheme where every branch has a prefix, so a bare release never exists.
Exercise
A teammate on macOS pushes Feature/Search. You are on Linux and already have feature/search. What can go wrong, and what habit prevents it?
Show answer
Linux treats the two names as different branches, but a case-insensitive filesystem cannot store both as files. The teammate's clone may mix them up or fail to create one, and the remote now holds two branches that differ only by case. Use lowercase branch names everywhere.