Sub topic 5
Naming Branches
This chapter assumes you can create and switch branches with git switch -c and git switch, and that you know a branch is a pointer to a commit. Here you will learn which names Git accepts, which conventions teams use, and how to rename a branch safely.
What Git allows
A branch name is a reference name, stored under refs/heads/. Git rejects names that would break that storage or confuse its command line. These rules cover the common cases:
- No spaces,
~,^,:,?,*,[, or backslashes. - No
..and no@{anywhere in the name. - No leading
-, because Git would read it as an option. - No trailing
/,., or.lock, and no component starting with.. - The name
HEADis reserved for the special reference.
Slashes are allowed. Git stores feature/login as a file named login inside a feature directory. This has one side effect: you cannot have both a branch feature and a branch feature/login, because feature cannot be both a file and a directory.
Names are case-sensitive in Git, but your filesystem may not be. On default macOS and Windows setups, Fix and fix can collide in a local clone. Pick lowercase names to avoid this.
Common conventions
Most teams use a short prefix, a slash, and a lowercase, hyphen-separated description.
| Prefix | Use | Example |
|---|---|---|
| `feature/` | New functionality | `feature/login` |
| `fix/` or `bugfix/` | Bug fixes | `fix/null-cart-total` |
| `hotfix/` | Urgent production fixes | `hotfix/payment-timeout` |
| `release/` | Release preparation | `release/1.0` |
| `chore/` | Maintenance work | `chore/update-deps` |
Many teams also include a ticket number, such as feature/PROJ-142-login. The ticket lets tools link the branch to your tracker. Use whatever your team has agreed on. Consistency matters more than the particular scheme.
Renaming a branch
Use git branch -m to rename. With one argument it renames the current branch. With two arguments it renames the named branch.
git switch -c feture/login
git branch -m feature/login
git branch -vvThe first command creates a branch with a typo. The second renames it, and the third shows the result. Renaming moves the pointer's name and its reflog. It does not change any commit.
If you already pushed the old name, the rename is local only. The remote still has the old branch, and your upstream setting still points to it. Fix this by pushing the new name and deleting the old one:
git push -u origin feature/login
git push origin --delete feture/loginBefore you delete a remote branch, make sure nobody else is working on it.
Checking a name
If you are unsure whether a name is valid, ask Git:
git check-ref-format --branch "feature/login"Git prints the name when it is valid. If it is invalid, Git prints an error and exits with a non-zero status.
Exercise
You renamed a branch locally from fix/typo to fix/header-typo, but it had already been pushed. What two commands update the remote, and what is the first one's side effect on your local branch?
Show answer
Run git push -u origin fix/header-typo, then git push origin --delete fix/typo. The first command creates the new remote branch and sets your local branch's upstream to it, so it no longer tracks the old remote name.