Sub topic 6
Branch names and how long branches live
This chapter assumes you can create, push, and merge Git branches and open pull requests. It also assumes you know the trunk-based flow from the previous chapter, where main is the source of truth and every change reaches it through a short-lived branch. Here you will set a naming convention, set limits on branch lifetime, and enforce both.
Why names and lifetimes matter more on Salesforce
A branch name is a label that people and automation read. A good convention lets a reviewer see what kind of change a pull request holds. It lets a script decide what to do with the branch. It lets you find the branch again in six months.
Lifetime is the larger concern. Salesforce metadata is stored as XML, and files such as profiles, permission sets, page layouts, and flows change often. A long branch has more time to overlap with other branches, so it collides more. Two developers who each touch the same permission set for two weeks will probably produce a conflict that is tedious to resolve. The same two changes merged within two days rarely conflict. Short branches are the main way to reduce metadata conflicts, and the naming rules below support that.
A naming convention
Use one prefix, a slash, and a short description in lowercase words separated by hyphens.
| Prefix | Use | Branches from | Merges into | Target lifetime |
|---|---|---|---|---|
| `feature/` | New functionality or an enhancement | `main` | `main` | Days, ideally 1 to 3 |
| `bugfix/` | Non-urgent defect fix | `main` | `main` | Days |
| `hotfix/` | Urgent production fix | `main` | `main` | Hours |
| `chore/` | Dependency, tooling, or pipeline changes | `main` | `main` | Hours to days |
Every row has the same source and destination. That is intentional. In a trunk-based flow there are no long-running develop or release branches, so the prefix describes intent and review urgency. It does not describe where the branch sits in a hierarchy.
Some rules that keep names useful:
- Use lowercase letters, digits, and hyphens. Salesforce metadata names are case-sensitive, and file systems treat case differently. Mixed-case branch names cause confusing problems on macOS and Windows, where
Feature/Xandfeature/xcan collide locally. - Describe the outcome, not the person.
feature/case-routing-rulesis better thanfeature/maria-work. - Put a ticket ID in the name if your team tracks work in Jira or similar, for example
feature/CRM-142-case-routing-rules. If you do, make that choice for every branch. A convention that is applied only sometimes cannot be checked by a script. - Keep names under about 50 characters. Long names get truncated in terminal output and pull request lists.
- Avoid names that clash with a prefix. Git stores branch refs as paths, so you cannot have a branch named
featureand another namedfeature/login.
The scratch org alias is a good companion to the branch name. In the feature workflow, the branch feature/case-routing-rules used a scratch org aliased case-routing. Matching the two makes it obvious which org belongs to which branch.
Branch lifecycle
Each branch goes through the same stages.
Two stages are easy to skip. The first is starting from a current main. A branch cut from a stale local copy begins with a hidden difference from the trunk, and that difference turns into a conflict later. Always run git switch main and git pull --ff-only origin main before git switch -c. The --ff-only flag makes the pull fail instead of creating an unexpected merge commit.
The second is deletion. A merged branch has no further purpose, and leaving it around makes the branch list hard to read. Configure your Git host to delete the source branch automatically when a pull request merges. On GitHub this is the "Automatically delete head branches" repository setting. To clean up locally:
git fetch --prune origin
git branch --merged main | grep -v -E '^\*|main$' | xargs -r git branch -dThe first command removes local references to branches that no longer exist on the remote. The second deletes local branches already merged into main. The -d flag refuses to delete a branch with unmerged work, which protects you from mistakes. The xargs -r option is GNU-specific. On macOS, omit -r, and expect an error message if nothing matches.
Setting a lifetime limit
"Short-lived" needs a number to be enforceable. A practical rule is that a branch should merge within three working days of its first commit. Treat five days as a hard ceiling that triggers a conversation.
Some work does not fit in three days. A new object with fields, page layouts, permission changes, and flows can take longer. You have three options, in this order of preference:
- Slice the work. Merge the object and fields first, then the automation, then the layout. Each slice must be deployable by itself.
- Merge incomplete work that is hidden. Use a custom permission, a custom metadata flag, or a feature-flagged Apex path so unfinished code reaches
mainwithout changing user behavior. Salesforce users only see what the layout, permission, or flow activation exposes. The metadata can exist in production without being exposed. - Keep the branch, but sync daily. If neither works, merge
maininto the branch (or rebase it) every day. This keeps the divergence small even when the branch is long.
Option 2 needs care. Hidden code still has to pass tests and deploy. An unfinished Apex class that does not compile will fail validation, so "hidden" does not mean "broken."
Finding old branches
Check branch age regularly. This command lists remote feature and bugfix branches, oldest first, with the date of the last commit:
git fetch --prune origin
git for-each-ref --sort=committerdate \
--format='%(committerdate:short) %(refname:short)' \
refs/remotes/origin/feature refs/remotes/origin/bugfixAnything older than your limit needs a decision. Either the work is still active and should be sliced, or it is abandoned and should be deleted. A branch with no commits for two weeks is usually abandoned. Before deleting it, push the useful part to a pull request or save a note in the ticket. Deleting a branch is reversible only while the commits are still reachable, so confirm with the author first.
Enforcing the convention in CI
Conventions that rely on memory drift. Add a check to the pull request workflow that fails when the branch name does not match. This step reads the branch name from an environment variable rather than interpolating it directly into the script, so a crafted branch name cannot inject shell commands.
name: Check branch name
on:
pull_request:
branches: [main]
jobs:
branch-name:
runs-on: ubuntu-latest
steps:
- name: Validate branch name
env:
BRANCH: ${{ github.head_ref }}
run: |
pattern='^(feature|bugfix|hotfix|chore)/[a-z0-9]+(-[a-z0-9]+)*$'
if [[ ! "$BRANCH" =~ $pattern ]]; then
echo "Branch '$BRANCH' does not match $pattern"
exit 1
fi
echo "Branch name OK: $BRANCH"The pattern accepts feature/case-routing-rules and hotfix/fix-opportunity-trigger. It rejects Feature/CaseRouting, my-branch, and feature/double--hyphen. If you include ticket IDs, change the pattern to allow uppercase letters in the ticket part, for example ^(feature|bugfix|hotfix|chore)/[A-Z]+-[0-9]+-[a-z0-9]+(-[a-z0-9]+)*$.
You can add a second check for age. The pull request workflow can compare the date of the first commit on the branch with today's date and post a warning when it passes your limit. A warning is usually better than a failure here, because an old branch that is nearly done should not be blocked.
Common errors
- Branching from another feature branch. A branch cut from
feature/acarries all of A's changes. If A is rejected, B contains code nobody approved. Branch frommainunless two pieces of work truly depend on each other, and in that case merge the first one first. - Reusing a branch after merge. Starting new work on an old merged branch drags in history that already reached
main. Create a new branch each time. - Using the branch as an environment. Names like
feature/uat-fixessuggest that a branch represents a place. Environments belong to orgs. Branches represent changes. - One branch for a whole sprint. This recreates a release branch in disguise. It brings back the conflicts that short branches are meant to avoid.
- Renaming after a pull request is open. Renaming a branch on the remote closes the pull request on many hosts. Choose the name before you push.
Summary of rules
- Name branches
type/short-descriptionin lowercase with hyphens. - Always cut from an up-to-date
main. - Aim to merge within three working days. Escalate at five.
- Slice large work, or merge it behind a flag.
- Delete branches on merge, and prune stale ones weekly.
- Check the name in CI.
Exercise
A teammate has had feature/new-quote-flow open for nine days. It changes a flow, two permission sets, and a page layout. Other developers keep hitting conflicts on the permission sets. Name two actions you would take, and explain why each one reduces conflicts.
Show answer
First, slice the work. Merge the permission set changes and any deployable parts first, so those files stop being held on a long branch and other branches start from the updated versions. Second, if the flow is not finished, merge it behind a custom permission or leave it inactive, so it reaches main without being exposed to users. Merging in smaller pieces shortens the time the branch overlaps with others, which is what causes the conflicts. As a stopgap, the teammate should also merge main into the branch daily so the differences stay small.
Exercise
Which of these branch names pass the pattern ^(feature|bugfix|hotfix|chore)/[a-z0-9]+(-[a-z0-9]+)*$? (a) feature/case-routing-rules (b) Feature/case-routing (c) hotfix/fix-opportunity-trigger (d) feature/case_routing
Show answer
(a) and (c) pass. (b) fails because of the uppercase F. (d) fails because the pattern allows hyphens but not underscores.