Sub topic 2
Naming branches so a team can read them
This chapter assumes you can create, commit to, and push branches, and that you know a branch is a pointer to a commit. Git does not care what you call a branch, but your teammates, your tooling, and your future self do. A consistent naming scheme makes a list of 40 remote branches readable at a glance.
What a good name tells you
A useful branch name answers three questions: what kind of work is this, what is it about, and (optionally) which ticket tracks it. A common pattern is:
<type>/<short-description>
<type>/<ticket-id>-<short-description>Common types:
| Prefix | Use |
|---|---|
| `feature/` | New behavior for users |
| `fix/` or `bugfix/` | A bug fix |
| `hotfix/` | An urgent fix to production |
| `release/` | Preparing a numbered release |
| `chore/` | Maintenance with no behavior change |
| `docs/` | Documentation only |
Examples: feature/login-form, fix/PAY-212-rounding-error, release/1.4, hotfix/1.4.1.
Rules Git enforces
Git rejects some names outright. A branch name cannot contain spaces, ~, ^, :, ?, *, [, or \. It cannot contain .. or @{. It cannot start or end with /, end with .lock, or start with -. You can check a candidate name with a built-in command:
git check-ref-format --branch "feature/login form"; echo "exit: $?"
git check-ref-format --branch "feature/login-form"; echo "exit: $?"The first command prints an error and exits non-zero because of the space. The second prints the name and exits with 0.
Conventions that avoid trouble
- Use lowercase and hyphens. Some filesystems ignore case, so
Feature/Loginandfeature/logincan collide in a local checkout. - Keep names short but specific.
fix/null-cart-totalis better thanfix/bugorfix/the-bug-where-the-cart-total-is-null-after-coupon. - Include the ticket ID if you use an issue tracker. Many trackers link a branch to its ticket automatically when the ID appears in the name.
- Name the purpose, not the person.
feature/login-formsurvives a change of owner.alice/stuffdoes not. - Avoid reusing a name after a branch is merged. A fresh name keeps history and remote listings unambiguous.
The slash is a directory separator
Git stores branch names as paths under .git/refs/heads/. This has a consequence that surprises people: you cannot have both feature and feature/login-form, because feature would need to be both a file and a directory.
git branch feature
git branch feature/login-formThe second command fails with an error like cannot lock ref 'refs/heads/feature/login-form': 'refs/heads/feature' exists. Prefixes are a feature, but only if you never use the bare prefix as a branch name.
Make the convention enforceable
A convention that lives only in a wiki drifts. Two common ways to enforce it:
- Review habit. Reviewers ask for a rename before approval. Renaming a local branch is cheap:
git branch -m old-name new-name. A remote branch needs a push of the new name and deletion of the old one. - Automation. Hosting platforms and CI can reject pushes or pull requests whose branch names do not match a pattern, such as
^(feature|fix|hotfix|release|chore|docs)/[a-z0-9._-]+$.
Exercise
A teammate already has a branch named feature. You try to create feature/search-bar and Git reports an error. Why does it fail, and what is the simplest fix?
Show answer
Git stores branches as paths under refs/heads/. The branch feature is a file at that path, so it cannot also be a directory that holds search-bar. The simplest fix is to rename the existing branch to something specific, such as git branch -m feature feature/search-v1. Alternatively, choose a different prefix for the new branch.