Sub topic 7
Building a feature from branch to pull request
This chapter follows one change from an empty branch to an open pull request. It assumes you have read the chapters on project layout, org types, and the trunk-based flow. You should know basic Git (branch, commit, push), have the Salesforce CLI (sf) installed, and be logged in to a Dev Hub. A Dev Hub is the org that creates scratch orgs.
The example feature is a set of case routing rules. The same steps apply to an Apex class, a flow, or a custom field.
The shape of the workflow
A feature has one branch, one development org, and one pull request. The branch holds the source of truth for the change. The org is a workspace where you build and test. The pull request asks the team to merge the branch into main.
Two rules keep this flow safe.
- The org is disposable. The branch is not.
- Nothing is finished until it exists as files in Git.
Step 1: Start from an up-to-date main
Always cut the branch from the current main. If you branch from a stale copy, you will deploy and test against metadata that no longer matches what your teammates have merged.
git switch main
git pull --ff-only origin main
git switch -c feature/case-routing-rules--ff-only makes the pull fail instead of creating a surprise merge commit when your local main has diverged. A diverged local main usually means someone committed to it by accident. Fix that before you continue.
The branch name follows the convention from the previous chapter: a type prefix, a slash, and a short description in lowercase with hyphens.
Step 2: Create a development org
You need an org to build in. You have two common options.
| Option | Source tracking | Lifetime | Best for |
|---|---|---|---|
| Scratch org | On by default | 1 to 30 days | Features that can be described by a definition file |
| Developer sandbox | Available, but you must enable it | Until refreshed or deleted | Features that need production-like data or settings |
Source tracking is the CLI's record of what changed in the org and what changed locally. It is what lets sf project retrieve start know which components to pull.
To create a scratch org from the project's definition file:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias case-routing \
--duration-days 7The alias gives you a short name to pass to later commands. A seven-day duration is a deliberate choice. It matches the expected life of a short-lived branch, and an org that expires is a reminder to merge or abandon stale work.
Step 3: Deploy the branch to the org
A new scratch org is empty. Deploy the current source so the org matches your branch:
sf project deploy start --target-org case-routingWith no other flags, the command deploys what source tracking says is out of sync, which for a new org is everything in your package directories. If the deploy fails, read the first error. Missing dependencies, such as a field referenced by a flow but absent from the source, cause most failures. Fix those in source and deploy again.
If your project needs seed data or permission set assignments, script them. A new teammate should be able to reach a working org with a few commands and no tribal knowledge.
sf org assign permset --name Case_Router_Admin --target-org case-routing
sf org open --target-org case-routingStep 4: Build and test
There are two ways to make changes, and a feature often uses both.
- Code-first. Edit files in
force-appin your editor, then runsf project deploy startto send them to the org. - Click-first. Use Setup in the org, for example to build assignment rules or a flow, then pull the result into source.
For click-first work, preview what the org changed before retrieving it:
sf project retrieve preview --target-org case-routing
sf project retrieve start --target-org case-routingReview the retrieved files before you commit them. Setup changes can pull in more metadata than you meant to touch, such as a profile or a standard object file with many unrelated entries.
Run Apex tests in the org as you go, not only at the end:
sf apex run test --target-org case-routing --test-level RunLocalTests --result-format human --code-coverage --wait 20> Salesforce requires at least 75% Apex code coverage to deploy to production, but treat that as a floor. The pull request check in the next chapter enforces it, so find failures here first.
Step 5: Commit in small, readable pieces
Check what changed, then stage deliberately:
git status
git diff --stat
git add force-app
git commit -m "Add case routing rules"git add force-app is fine when you have reviewed the diff. If the retrieve added unrelated files, stage only the paths you mean to include, with git add -p or explicit paths.
Good commit habits for Salesforce projects:
- Commit the Apex class and its test class together, so every commit is deployable.
- Keep formatting-only changes in a separate commit. Metadata XML diffs are already noisy.
- Do not commit
.sfdx/,.sf/, ornode_modules/. They belong in.gitignore.
Step 6: Sync with main before you push
While you worked, other branches may have merged. Bring those changes in now. A conflict you find on your machine costs minutes. The same conflict found after review costs a day.
git fetch origin
git rebase origin/mainIf your team prefers merge commits over rebasing, use git merge origin/main instead. Either is fine, but the whole team should use the same one. If the sync brings in metadata you did not have, redeploy to your scratch org and rerun the tests. Passing tests on an old base prove little about the merged result.
Step 7: Push and open the pull request
git push -u origin feature/case-routing-rulesThe -u flag links your local branch to the remote one, so later pushes need only git push. If you rebased after an earlier push, use git push --force-with-lease. It refuses to overwrite commits you have not seen.
Then open a pull request into main in your Git host. If you use GitHub's CLI, you can do it from the terminal:
gh pr create --base main --title "Add case routing rules" --body "Adds assignment rules for the support queue. Tested in scratch org case-routing."A useful description answers four questions:
- What changed, and why?
- Which metadata types are involved?
- How was it tested?
- Are there manual steps after deployment, such as activating a flow or assigning a permission set?
Manual steps are the most commonly missed item. Metadata cannot carry everything, and the release owner needs them written down.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `retrieve start` reports no changes | The org has no source tracking, or you changed a type that tracking does not cover | Retrieve explicitly with `--metadata` or `--source-dir` |
| Deploy works locally but fails in the pull request check | The branch is behind `main`, or a dependency is not in source | Sync with `main`, then redeploy and retest |
| The diff shows hundreds of changed lines in one file | A profile or other large file was retrieved in full | Revert the file and keep only intended edits, or use permission sets instead |
| Push is rejected as non-fast-forward | You rebased a branch that was already pushed | Use `git push --force-with-lease` on your own branch only |
| Scratch org creation fails | The Dev Hub is out of scratch org allocation, or the definition file has an invalid feature or edition | Delete unused orgs and check `config/project-scratch-def.json` |
Cleaning up
When the pull request merges, delete the branch and the org:
git switch main
git pull --ff-only origin main
git branch -d feature/case-routing-rules
sf org delete scratch --target-org case-routing --no-promptLeaving old branches and orgs around makes it hard to tell which work is live. It also uses up your Dev Hub's active scratch org limit.
Exercise
You built a flow in a scratch org's Setup UI, committed your Apex changes, and pushed the branch. The pull request deploys fine, but the flow is missing from main after the merge. What step did you skip, and what command would have caught it?
Show answer
You skipped retrieving the flow from the org into source, so it was never in the commit. sf project retrieve preview --target-org <alias> shows changes that exist in the org but not locally, and sf project retrieve start --target-org <alias> pulls them in. Run the preview before each commit, then check git status for the new flow file.
Exercise
Why does this workflow use git pull --ff-only on main and a short --duration-days for the scratch org?
Show answer
--ff-only stops local main from silently diverging, so each feature branch starts from the same history as the remote. A short scratch org duration matches the short life of a feature branch and ensures that old orgs expire instead of accumulating unreviewed, untracked changes.