Sub topic 13
Detecting and Reconciling Org Drift
This chapter assumes you know the trunk-based flow from earlier chapters, can use the Salesforce CLI, and understand the difference between production, sandboxes, and scratch orgs.
What org drift is
Org drift is any difference between what main says an org should contain and what the org actually contains. Drift appears when someone changes metadata directly in an org, for example an admin editing a flow in production during an incident, or a developer fixing a validation rule in a UAT sandbox.
Drift matters because the next deployment from main overwrites the org. A hand-made fix can disappear without warning, or a deployment can fail because the org now differs from what the source expects.
Detect drift
Retrieve the org's current metadata into a scratch directory, then compare it with main. Do this on a throwaway branch so you never commit by accident.
git switch main
git pull --ff-only origin main
git switch -c chore/drift-check-prod
sf project retrieve start --target-org prod --source-dir force-app
git status --short
git diff --statIf the working tree is clean, the org matches main for the metadata you retrieved. Any modified or untracked file is drift.
Retrieve only the directories you track. Retrieving everything produces noise from managed packages and metadata that your project does not own.
The Salesforce CLI can also report changes tracked by source tracking in sandboxes and scratch orgs. Production does not support source tracking, so the retrieve-and-diff approach is the general method.
Decide what to do with each difference
Read each difference and choose one of two outcomes.
| Situation | Action |
|---|---|
| The org change is wanted | Back-propagate it into `main` through a normal pull request |
| The org change is unwanted or accidental | Leave `main` as is. The next deployment restores the intended state |
Back-propagate a wanted change
Back-propagation means moving a change made in an org into Git. Treat it like any other feature. It needs a branch, a pull request, validation, and review.
git switch main
git pull --ff-only origin main
git switch -c fix/backport-prod-flow-change
sf project retrieve start --target-org prod --metadata Flow:Case_Escalation
git add force-app
git commit -m "Back-propagate Case_Escalation flow change from production"
git push -u origin fix/backport-prod-flow-changeOpen a pull request into main. Name the person who made the org change and explain why in the description. Reviewers can then confirm the change is intended.
Because the change is already live in production, the merge brings Git in line with production. It does not need a new production deployment, though it still goes through the lower environments so they receive the change too.
Reduce drift at the source
Detection is a safety net. Prevention is cheaper.
- Limit who can edit metadata in production and UAT. Use permission sets to restrict "Customize Application" for most users.
- Run the drift check on a schedule, for example nightly, and report differences to the team.
- Make the pull request path fast enough that people do not bypass it.
- After a hotfix, check that the change reached
main. The hotfix chapter covers this path.
Common errors
- Committing a retrieve straight to
main. This skips review and validation. Always use a branch. - Retrieving too much. Managed package metadata and unrelated components flood the diff.
- Ignoring drift in lower environments. A drifted UAT sandbox makes test results misleading, because UAT no longer matches what production will receive.
Exercise
A nightly drift check on production shows a modified validation rule that is not in main. A system administrator confirms they changed it during an incident and the change should stay. What are the steps to reconcile it?
Show answer
Create a branch from an up-to-date main, retrieve the validation rule from production with sf project retrieve start, commit it, and open a pull request into main describing who changed it and why. Let validation and review run, then merge. Production already has the change, so no production deployment is needed, but lower environments receive it on their next deployment from main.