Sub topic 12
Shipping a Hotfix Safely
This chapter assumes you know the trunk-based flow from earlier chapters: main is the source of truth, feature branches are short-lived, and pull requests run a check-only deployment before merge. You should also know how to authenticate the Salesforce CLI to an org and read basic git output.
A hotfix is a change that cannot wait for the normal release cadence. A trigger throws an error on every Opportunity save, a validation rule blocks order entry, or a flow sends the wrong email. The goal is to fix production quickly without skipping the checks that keep the fix from causing a second incident.
Why hotfixes go wrong on Salesforce
Hotfixes in Salesforce projects tend to fail in a few predictable ways:
- Editing production directly. An admin or developer patches the org through Setup or the Developer Console. The fix works, but Git does not contain it. The next deployment from
mainoverwrites it, and the bug returns. - Branching from the wrong place. If the hotfix branch starts from a feature branch or from a UAT-only branch, it carries unreleased work into production.
- Skipping validation. Production deployments with Apex require tests. A fix that was never validated can fail at the worst moment, or pass without tests and break something else.
- Losing the fix in other branches. Feature branches cut before the fix do not contain it. When they merge later, they can reintroduce the old code.
The flow below addresses each of these. It adds almost nothing to the normal process, which is the point. A hotfix should use the same path as any change, only faster.
The hotfix flow
In a trunk-based model, main always reflects what is intended for production. So a hotfix branches from main, merges back to main through a pull request, and is then deployed and tagged.
Step 1: Branch from an up-to-date main
Start from the current state of main, not from whatever branch you happen to have checked out.
git switch main
git pull --ff-only origin main
git switch -c hotfix/fix-opportunity-triggerThe --ff-only flag makes the pull fail if your local main has diverged. That is a useful alarm. If it fires, something was committed to local main that should not be there, and you want to know before you build on it.
Step 2: Reproduce and fix
Reproduce the problem somewhere safe. A scratch org or a full or partial sandbox that mirrors production metadata is a good choice. Write a failing Apex test first when the bug is in code. The test proves the fix works and stays in the repository to prevent a repeat.
Keep the change small. A hotfix is not the place to rename a class, clean up formatting, or add a field "while you are in there." Each extra file adds review time and deployment risk.
Step 3: Open a pull request and let validation run
Push the branch and open a pull request into main.
git add force-app
git commit -m "Fix null reference in OpportunityTrigger"
git push -u origin hotfix/fix-opportunity-triggerThe same validation workflow that runs for features should run here. It performs a check-only deployment, which compiles and tests the change in the validation org without saving it.
name: Validate pull request
on:
pull_request:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to the validation org
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
printenv SFDX_AUTH_URL > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias ci-org
rm auth-url.txt
- name: Check-only deploy with local tests
run: sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTests --target-org ci-org --wait 60Do not bypass this check because the incident is urgent. If the full test run is too slow for your situation, speed it up in advance. Options include a delta deployment that sends only changed components, or a targeted test list. Both are covered in later chapters. Decide on them before the incident, not during it.
Step 4: Review and merge
Branch protection still applies. A hotfix needs at least one reviewer, ideally someone who did not write it. During an incident, a second pair of eyes is cheap compared to a failed deployment.
If your team has an emergency path, define it in writing. For example, allow a single approval from a named on-call reviewer instead of two. Keep the passing validation as a hard requirement. Avoid giving administrators a standing bypass, because that is the first thing used under pressure.
Step 5: Deploy to production and tag
After the merge, deploy main to production using your normal release job. Then tag the commit that was deployed.
git switch main
git pull --ff-only origin main
git tag -a v2026.12.1 -m "Hotfix: opportunity trigger"
git push origin v2026.12.1An annotated tag (-a) records who tagged it, when, and why. The tag gives you a precise answer to "what is in production right now?" and a clear rollback target if the hotfix itself has a problem.
Keeping in-flight work safe
While the hotfix is in progress, other developers have feature branches open. Two things must hold:
- The hotfix must not wait on or include their work.
- Their branches must eventually contain the fix.
The first is satisfied by branching from main. For the second, ask developers to update their branches from main after the hotfix merges.
git switch feature/case-routing-rules
git fetch origin
git merge origin/mainMerging is usually safer than rebasing for a branch that others have pulled. If the feature touches the same file as the hotfix, you will get a conflict. Resolve it the way you would any metadata conflict, and re-run the tests afterward. A merge that applies cleanly can still break behavior, so do not skip the test run.
If your team keeps a UAT branch or a long-lived staging environment, the hotfix must also reach it. Deploy main to that sandbox as well, so UAT does not test against code that production no longer runs.
When the fix is declarative
Many production incidents are caused by configuration, not code: a flow, a validation rule, a permission set, a page layout. The temptation to fix these in Setup is strong because it takes two minutes.
If you must change production in Setup to stop the damage, treat it as a temporary measure, not the fix. Then:
- Retrieve the change into your branch with
sf project retrieve start. - Commit it on a hotfix branch.
- Send it through the pull request, validation, and merge.
This closes the gap between the org and Git. Without it, the org has drifted from main, and the next deployment may overwrite the manual change or fail on it. Drift is covered in its own chapter.
git switch main
git pull --ff-only origin main
git switch -c hotfix/restore-case-validation-rule
sf project retrieve start --source-dir force-app/main/default/objects/Case --target-org production
git status
git add force-app
git commit -m "Capture Case validation rule fix made in production"Review the retrieved diff before committing. A retrieve from production can pull in unrelated changes that other people made in Setup. Commit only the files that belong to the fix.
Rolling back
Sometimes the right response is to undo the last release. Because every release is tagged, you can create a revert through a normal pull request.
git switch main
git pull --ff-only origin main
git switch -c hotfix/revert-opportunity-trigger
git revert <commit-sha>
git push -u origin hotfix/revert-opportunity-triggerA revert is a new commit that undoes an earlier one, so history stays intact. Two Salesforce-specific cautions apply:
- Deploying an older version of a component does not remove components that the newer version added. Deleting a field, class, or flow version requires a destructive change, and deleting a custom field can destroy data.
- Some metadata cannot be moved backward easily. An active flow version can be replaced, but a deployed picklist value or field type change may not roll back cleanly.
Check what the failed release added before you rely on a revert.
Common mistakes
| Mistake | Consequence | Prevention |
|---|---|---|
| Branching from a feature or UAT branch | Unreleased work reaches production | Always start from an up-to-date `main` |
| Fixing in production only | Next deployment erases the fix | Retrieve and commit the change the same day |
| Bypassing validation | Failed or unsafe production deployment | Keep a fast, pre-agreed validation path |
| Not tagging | No clear record of what was deployed | Tag every production deployment |
| Not updating feature branches | The bug returns when they merge | Announce the merge and have developers merge `main` |
Exercise
A production flow is sending incorrect emails. An admin disables the faulty flow in Setup to stop the damage. What should the team do next to keep Git and the org consistent, and why?
Show answer
Retrieve the changed flow metadata from production into a hotfix branch cut from an up-to-date main, review the diff so that only the intended change is committed, and open a pull request into main. Let the check-only validation pass, get a review, merge, deploy through the normal release job, and tag the release. Without this, production differs from main, so the next deployment from main could re-enable the faulty flow or overwrite the manual change.
Exercise
Your hotfix merged to main two hours ago. A teammate's feature branch was created last week and has not been updated. What should the teammate do before opening a pull request, and what risk does this avoid?
Show answer
Fetch and merge origin/main into the feature branch, resolve any conflicts, and re-run the tests. This brings the hotfix into the feature branch. Otherwise the feature could reintroduce the old, broken code when it merges, or conflict with the fix at merge time.