Sub topic 10
Resolving conflicts deliberately
This chapter assumes you have read the earlier chapters on previewing, deploying, and retrieving tracked changes, and on how conflicts arise. You should be comfortable with sf project deploy and sf project retrieve, a scratch org or sandbox with tracking enabled, and basic Git commands (status, diff, commit, restore).
A conflict means the same component changed in your project and in the org since the last sync. The CLI stops because either direction would overwrite someone's work. This chapter covers how to decide which version wins, or how to combine both, and how to do it so you can undo a mistake.
What the CLI is protecting
Source tracking compares three things for each component:
- The local file, checked against the snapshot the CLI stored after the last deploy or retrieve.
- The org's current revision for that component, checked against the revision recorded at the last sync.
- The two sides against each other.
If only one side changed, the CLI moves the change. If both sides changed, it reports a conflict and refuses to run the command. The refusal is the safety feature. The flag that turns it off, --ignore-conflicts, decides the winner without looking at either version.
A conflict does not say which version is correct. You decide that, and the CLI only lets you do it with full information.
Step 1: Look before you act
Start every resolution with the two preview commands. They change nothing in the org or your project.
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchEach preview prints a table of components. Read it for three things:
- Components marked as conflicts. These are the ones that block a plain deploy or retrieve.
- Components that are listed on only one side. These will move normally and need no decision.
- Components you do not recognize. A teammate or an admin working in Setup probably made that change.
Run both previews, because a conflict appears in both. The deploy preview shows what would be pushed, and the retrieve preview shows what would be pulled. A component in both lists with a conflict flag is the one you need to resolve.
Step 2: Choose a resolution
Three outcomes are possible, and each one has a different command sequence.
| Outcome | When to use it | What happens to the other side |
|---|---|---|
| Local wins | Your version is correct and the org change is a mistake or a draft | The org change is overwritten on deploy |
| Org wins | The org change is correct and your local edit is obsolete | Your local file is overwritten on retrieve |
| Merge | Both changes are valid and touch different parts of the component | Neither is lost, but you edit the file by hand |
Local wins
Use this when you have confirmed the org change should be discarded. For example, a teammate tested a change in the org and abandoned it.
sf project deploy start --target-org my-scratch --ignore-conflictsThe deploy overwrites the org's version of every conflicting component in the deployment. Before you run it, confirm in the preview that the conflict list contains only components you intend to overwrite. The flag applies to all of them. If one of the conflicts is a change you did not know about, resolve that one separately first.
The org's earlier version is gone after this deploy. If you are not sure the org change is worthless, retrieve a copy first. The merge procedure below shows how.
Org wins
Use this when the org change is correct and your local edit should go. Retrieving with --ignore-conflicts overwrites your local files, so protect your work first.
git add -A
git commit -m "WIP: local edits before taking org version"
sf project retrieve start --target-org my-scratch --ignore-conflictsThe commit is the undo button. If you retrieve and then realize you needed a local edit, git show HEAD:path/to/file or git restore --source=HEAD~0 brings it back. Without the commit, the overwritten edits cannot be recovered, because the CLI does not keep backups of your files.
Merge
When both sides contain wanted changes, use Git as the merge tool. The CLI cannot merge. It only moves whole components.
- Commit your local work so the working tree is clean.
- Retrieve the org version over your files with
--ignore-conflicts. - Use
git diffto see exactly how the org's version differs from what you committed. - Edit the files so they contain both sets of changes.
- Commit the result, then deploy.
git add -A
git commit -m "Local version of OrderService before merge"
sf project retrieve start --target-org my-scratch --ignore-conflicts
git diffAfter the retrieve, git diff shows the org's changes as edits on top of your commit. Lines starting with - are your local code, and lines starting with + are what the org had. You can keep chosen hunks with git restore -p --source=HEAD path/to/file, which lets you pick hunks from your committed version one at a time. When the file reads the way you want, deploy it:
git add -A
git commit -m "Merge org and local changes to OrderService"
sf project deploy start --target-org my-scratchThis deploy needs no --ignore-conflicts. The retrieve updated the tracking state to say the local file matches the org revision. Your hand edits are then ordinary local changes, so they deploy normally.
Checking that the conflict is gone
After any of the three resolutions, run the previews again.
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchYou want the conflicted components to disappear from both lists, or to appear only as ordinary local changes if you edited them after the merge. If a component still shows as a conflict, someone changed it in the org again while you worked. That is common in a shared sandbox, and the answer is to repeat the process with the newer org version.
When the histories no longer match
Sometimes conflicts appear for components you never touched, or the preview reports conflicts that do not match any real difference. This happens after you restore an old Git branch, copy a project folder, or delete the tracking files. The CLI's record of the last sync no longer describes either side.
If you have already compared the two sides and know they are the same, you can tell the CLI to treat them as synchronized:
sf project reset tracking --target-org my-scratchThis resets the tracking state to the chosen org revision. It does not move any files, so it is only safe when the local project and the org really match. If they differ, the differences become invisible to tracking, and later deploys and retrieves will skip them. Retrieve and diff first. Reset second.
A habit that prevents most conflicts
Most conflicts come from a short list of causes: edits made in Setup, a teammate sharing the same org, and local work that was never deployed before switching branches. You can cut them down with a routine:
- Run
sf project retrieve previewbefore you start editing, so you begin from a known state. - Retrieve org-side changes and commit them before you start a new feature.
- Avoid sharing one scratch org between several developers. Give each person an org.
- Deploy small and often, so the window for a collision is short.
Exercises
Exercise
A teammate edited the Apex class PricingService in the shared sandbox. You also edited it locally. sf project deploy start fails with a conflict. You believe both edits are needed. List the commands, in order, that you would use to combine the changes without losing either.
Show answer
First git add -A and git commit to save your local version. Then sf project retrieve start --target-org <alias> --ignore-conflicts to bring the org version in, which is safe because your edit is committed. Use git diff and git restore -p --source=HEAD <file> or manual editing to combine both sets of changes. Commit the merged file, then run sf project deploy start --target-org <alias>, which no longer needs --ignore-conflicts because the retrieve resynchronized tracking.
Exercise
After checking out an old branch, the preview lists fifteen conflicts. You compared the files with the org and found they differ in two components. Why is running sf project reset tracking immediately a bad idea, and what should you do first?
Show answer
The reset makes the CLI treat the project and org as identical, so the two components that differ would stop appearing as changes and could be skipped by later deploys or retrieves. First resolve the two real differences (retrieve and merge, or deploy the correct version), confirm both previews are clean, and reset only if the remaining conflicts are false reports.