Sub topic 9
How conflicts arise
This chapter assumes you have a Salesforce DX project, the sf CLI installed, and an authorized scratch org or tracked sandbox. It also assumes you can run basic Git commands and that you know what an Apex class or a custom object is. Earlier chapters covered how tracking records changes. This chapter explains what happens when both sides change the same component.
What a conflict is
A conflict is a component that has changed in your local project and in the org since the last time the two were synchronized. The CLI cannot tell which version you want, so it stops instead of choosing one.
Source tracking keeps two lists of changes for a tracked org:
- Local changes: files in your project that differ from the last synchronized snapshot.
- Remote changes: components in the org whose revision number is newer than the last one the CLI recorded.
A component on both lists is a conflict. A component on only one list is a normal change. The CLI deploys it or retrieves it without complaint.
The check is per component, not per project. If you edited AccountService locally and someone else edited ContactService in the org, nothing conflicts. If you both edited AccountService, you have a conflict.
Why the CLI checks both directions
Conflict detection protects against silent data loss in both directions.
- On deploy, it protects the org. A remote change that you have not retrieved would be overwritten by your local file.
- On retrieve, it protects your working directory. A local edit that you have not deployed would be overwritten by the org version.
Both commands run the same check before they move anything. That is why sf project deploy start can fail with a conflict even though you only wanted to push, and why sf project retrieve start can fail even though you only wanted to pull.
A conflict, step by step
The sequence below produces a conflict from a clean state. Assume a scratch org named my-scratch that is fully synchronized with your project.
- You edit
force-app/main/default/classes/AccountService.clslocally. The CLI now sees one local change. - A teammate, or you in another browser tab, edits
AccountServicein the org through Setup or the Developer Console. The org records a newer revision for that class. - You run
sf project deploy start --target-org my-scratch. - The CLI compares its lists, finds
AccountServiceon both, and stops with a conflict error. Nothing is deployed.
The diagram shows the same sequence.
The deploy fails before any component moves. That is the safe outcome. The risk is in the next step, when someone adds an override flag without reading what is being overwritten.
Seeing a conflict before it hurts
The preview commands report conflicts without changing anything. Run both before a risky push or pull:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe output groups components by what would happen to them. Components marked as conflicts are the ones you must decide about. The same component often appears in both previews, once as something you would deploy and once as something you would retrieve. That double appearance is the signature of a conflict.
Common ways conflicts arise
Most conflicts come from a short list of habits and situations.
| Situation | Why it conflicts |
|---|---|
| Editing in Setup while also editing locally | Both sides change the same component after the last sync. |
| Several developers sharing one scratch org | Each developer's local project is behind the org changes made by others. |
| Pulling new commits from Git | The merged files change locally, so components that also changed in the org are now on both lists. |
| Switching Git branches | Files change on disk without any edit from you. The CLI reads them as local changes. |
| Automated processes changing metadata | Some org features update metadata themselves, such as a permission set after a field is added through Setup. |
| Editing a component after a failed deploy | The remote state may be partly updated, so the two sides no longer match. |
Two rows deserve more attention.
Git operations count as local changes
The CLI does not know about Git. It compares file contents on disk with its stored snapshot. When you run git checkout or git pull, every file that changes on disk looks like a local edit made by you.
Suppose you switch from feature-a to feature-b. Files that differ between the branches now appear as local changes. If any of those components were also changed in the org, the CLI reports conflicts you did not create by typing anything.
A practical habit is to run sf project deploy preview after switching branches. The output tells you whether your branch change touched components that the org also changed.
Metadata that changes itself
Some changes in the org come from Salesforce, not from a person. Adding a custom field in Setup can update a profile or permission set. A package install can modify existing components. These count as remote changes. If you also have local edits to the same profile, you get a conflict that no teammate caused.
Profiles are the usual source of this. They are large files that many unrelated actions touch, so they conflict more often than most metadata types.
What a conflict is not
Two misreadings are common.
- A conflict is not a merge conflict. Git merge conflicts happen between two versions of a text file, and Git marks the lines. A source tracking conflict is a flag on a whole component. The CLI does not merge the contents and does not mark lines.
- A conflict does not mean the files differ. Tracking compares revisions, not contents. If you edit a class in the org and then make the identical edit locally, the CLI still reports a conflict, because both sides changed after the last sync.
The second point surprises people. It also means that a conflict can be harmless. The two versions might be identical, or differ only in whitespace. You still have to resolve it, but the resolution is quick.
Conflicts require tracking
Only tracked orgs, such as scratch orgs and sandboxes with tracking enabled, detect conflicts. In an org without tracking, the CLI has no record of revisions. It deploys exactly what you name and overwrites whatever is in the org. No warning is given. The safety check described in this chapter is a benefit of tracking, and without it you have to look for differences yourself. A later chapter covers that workflow.
The override flags
Both deploy and retrieve accept --ignore-conflicts. It tells the CLI to skip the check and let one side win:
sf project deploy start --target-org my-scratch --ignore-conflicts
sf project retrieve start --target-org my-scratch --ignore-conflictsThe two commands do opposite things:
- Deploy with the flag: your local version replaces the org version.
- Retrieve with the flag: the org version replaces your local file.
The overwritten version is gone unless it exists elsewhere. Your local file may be safe in Git if you committed it, but the org version has no backup unless you retrieved it earlier. For that reason, treat the flag as the last step of a decision, not as a way to make an error message disappear. The next chapter covers how to decide.
Reading the signs early
You can often predict conflicts before they appear.
- If more than one person uses an org, assume conflicts will occur and retrieve often.
- If you edit in Setup, retrieve right afterward so that your project matches the org before you touch the same component locally.
- If you changed branches or pulled, preview before you deploy.
- If a profile or permission set is involved, expect extra noise and check it first.
Summary of the mechanism
A conflict needs three things: a tracked org, a component changed locally since the last sync, and the same component changed remotely since the last sync. Remove any one and there is no conflict. The CLI detects it by comparing revision records, not file contents. Git actions, Setup edits, and automatic metadata updates all produce changes, and the CLI treats each the same way.
Exercise
You switch Git branches, and then sf project deploy start reports a conflict on a custom object. You did not edit that object by hand, and a teammate says they changed it in the org yesterday. Explain how the conflict arose and name the safest first step.
Show answer
Switching branches changed the object's file on disk, and the CLI treats any on-disk change as a local change. The teammate's edit in the org is a remote change to the same component, so it appears on both lists and the CLI reports a conflict. The safest first step is to run sf project deploy preview and sf project retrieve preview to see what each side would change, without using --ignore-conflicts.
Exercise
You edit a class in the org, then make the exact same edit in your local file. Will the CLI report a conflict? Give the reason.
Show answer
Yes. Tracking compares revisions, not file contents. Both sides changed after the last synchronization, so the component is on both change lists even though the contents match.