Sub topic 13
Detecting and reconciling org drift
Org drift is the state where an org holds metadata that differs from what main says it should hold. This chapter covers where drift comes from, how to detect it on a schedule, and how to reconcile it without losing work or overwriting a deliberate change.
It assumes you can run sf commands against a sandbox, know what main and a pull request are, and have read the chapters on org types and the feature workflow.
Where drift comes from
In a trunk-based flow, main is the source of truth and orgs receive changes only through deployments. Drift appears when something else changes an org.
- Direct edits in Setup. An admin changes a validation rule in production to unblock sales, and nobody records it in Git.
- Emergency fixes. A hotfix is made in production first, and the follow-up pull request is forgotten.
- Change sets and manual deployments. Someone deploys from a developer's laptop or an unmerged branch.
- Long-lived sandboxes. A UAT sandbox keeps changes from an abandoned feature that never merged.
- Platform-generated changes. Some metadata, such as profile or permission set contents, changes when you install a package or turn on a feature.
Drift matters because the next deployment from main can overwrite the direct edit. A validation rule that an admin fixed by hand then silently reverts. In the other direction, a feature that was tested against a drifted sandbox can fail in production, where the drift is absent.
Detecting drift
Detection works by retrieving the org's metadata into a clean checkout of main and asking Git what changed. Git is the comparison tool, so you need no special software.
Source tracking helps only in some orgs
Scratch orgs and Developer or Developer Pro sandboxes support source tracking. For these, the CLI records what changed in the org since your last sync, and you can preview it:
sf project retrieve preview --target-org case-routingThe command lists remote changes that a retrieve would bring in, plus conflicts with local files. Production, Partial Copy, and Full sandboxes do not support source tracking. For those orgs you retrieve by manifest and compare with Git.
Retrieve and diff
The manifest names the metadata types to compare. A small manifest/package.xml is enough to start:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<types>
<members>*</members>
<name>ApexTrigger</name>
</types>
<types>
<members>*</members>
<name>CustomObject</name>
</types>
<types>
<members>*</members>
<name>Flow</name>
</types>
<types>
<members>*</members>
<name>ValidationRule</name>
</types>
<version>62.0</version>
</Package>Then run the comparison locally from a clean branch cut from main:
git switch main
git pull --ff-only origin main
git switch -c drift/uat-2026-12-01
sf project retrieve start --manifest manifest/package.xml --target-org uat --wait 30
git status --short
git diff --statThe retrieve overwrites files in force-app with the org's version. Anything git status reports is a difference between main and UAT. Modified files are components that changed in the org. Untracked files are components that exist only in the org. Files present in main but missing from the org will not show as deleted, because a retrieve never removes local files. Deleted components therefore need a separate check, such as listing the org's components with sf org list metadata and comparing the names.
Detect on a schedule
Checking by hand is easy to forget. A scheduled workflow does the same retrieve and fails if anything differs:
name: Detect org drift
on:
schedule:
- cron: "0 6 * * 1-5"
workflow_dispatch:
jobs:
drift:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: main
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to the org
env:
SFDX_AUTH_URL: ${{ secrets.UAT_SFDX_AUTH_URL }}
run: |
printenv SFDX_AUTH_URL > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias uat
rm auth-url.txt
- name: Retrieve metadata from the org
run: sf project retrieve start --manifest manifest/package.xml --target-org uat --wait 30
- name: Fail if the org differs from main
run: |
git add -A
if ! git diff --cached --quiet; then
git diff --cached --stat
git diff --cached > drift.patch
exit 1
fi
- name: Keep the drift patch
if: failure()
uses: actions/upload-artifact@v4
with:
name: drift-patch
path: drift.patchA failed run is the alert. The uploaded patch shows exactly which lines differ, so the person on call can start triage without logging in to the org.
Find out who and when
Git tells you what differs, but not who changed it. For that, use Setup Audit Trail in the org (Setup, then View Setup Audit Trail). It lists recent setup changes with the user and timestamp, and you can download the last 180 days. Match the file names in the patch to the entries to find the person to ask.
False positives
A first drift run often reports dozens of differences that nobody made. Common causes:
- Reordered or reformatted XML. Profiles and permission sets are the usual offenders. The CLI retrieves them in a stable format, but older files in
mainmight have been saved by a different tool or API version. - API version differences. Components retrieved under a different
sourceApiVersioncan carry extra fields. - Components you do not own. Managed package contents and some standard objects change when the vendor ships an update. Add them to
.forceignoreso they stop appearing. - Environment-specific values. Named credentials, custom settings, and email addresses legitimately differ between orgs. Exclude them from the manifest or handle them with per-environment configuration.
Clear the noise once. Retrieve, commit the formatting-only differences to main through a normal pull request, and extend .forceignore where needed. After that, every alert is a real signal. A drift check that always fails gets ignored, which is the same as having none.
Reconciling drift
For each real difference, decide whether the org or main is right. The decision is a team call, and the table lists the usual outcomes.
| Situation | Decision | Action |
|---|---|---|
| The change fixes a real problem and should stay | Accept | Back-propagate through a pull request into `main` |
| The change was a hotfix made in production | Accept | Back-propagate, then tag per the hotfix process |
| The change was an experiment or mistake | Reject | Redeploy `main` to the org to overwrite it |
| The change comes from a vendor or package update | Ignore | Add to `.forceignore`, or pin the version |
Back-propagation
To accept a change, keep the retrieved files and move them into Git as an ordinary change:
git switch drift/uat-2026-12-01
git add force-app
git commit -m "Back-propagate validation rule change from UAT"
git push -u origin drift/uat-2026-12-01Open a pull request into main. It goes through the same check-only validation and review as a feature, so drift does not get a shortcut. Review the diff and remove unrelated lines before you commit. Then the change reaches the other orgs through promotion, and the drifted org matches main again once the merge deploys.
Reverting
To reject a change, deploy main over the org. Use a scoped deployment so you only overwrite the components you reviewed:
git switch main
git pull --ff-only origin main
sf project deploy start --source-dir force-app/main/default/objects/Case --target-org uat --wait 30Tell the person who made the edit before you overwrite it, and write down the reason. Reverting a change without telling anyone usually returns as the same edit a week later.
Reducing drift
Detection is a safety net. Fewer drift sources mean fewer alerts.
- Limit who can change metadata directly in UAT and production. Remove "Customize Application" from users who do not need it.
- Use an agreed route for emergencies, and require the back-propagation pull request within a set time, such as one working day.
- Refresh long-lived sandboxes from production on a schedule, so leftover changes from abandoned work disappear.
- Make the drift report visible. A failed workflow that sits unread has no effect.
Exercise
A scheduled drift check against production fails. The patch shows a modified validation rule on Opportunity and a new Flow that does not exist in main. Setup Audit Trail shows an admin created both during an outage last week. What steps do you take, and why do you not simply deploy main to production?
Show answer
Deploying main would overwrite the validation rule and, depending on the deployment, leave the Flow orphaned or remove the fix that resolved the outage. Instead, create a branch from main, retrieve the two components from production, review the diff, and commit them. Open a pull request into main so the change gets check-only validation and review. Ask the admin whether the change should stay. If yes, merge and promote it. If not, redeploy main for those components only and record why. Afterward, require the back-propagation pull request in the emergency procedure so the gap does not recur.