Sub topic 14
Combining Source Tracking with Git in a Team
This chapter assumes you know the basic Salesforce DX project layout, have the sf CLI installed with an org authorized, and can use Git branches, commits, and merges. It also assumes you have read the earlier chapters on previewing, deploying, retrieving, and resolving conflicts.
Two histories, two jobs
Git and source tracking record different things. Git records the history of your project files and is shared by the whole team. Source tracking records the state of one specific org against one local project copy, and it is stored on your machine.
- Git answers "what changed in the project, and who changed it?"
- Source tracking answers "what differs between my files and this scratch org?"
Neither replaces the other. Git is the source of truth for your team. A tracked org is a personal workspace that you fill from Git and empty back into Git.
One scratch org per developer
Because tracking state lives in your local .sf directory, each developer should work in their own scratch org. Sharing one tracked org between people produces conflicts, because the org's revision counters change when a colleague deploys and your local snapshot knows nothing about it.
git switch -c feature/invoice-validation
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default
sf project deploy start --target-org my-scratchThe first deploy sends the whole project because no tracking history exists yet. After that, deploys send only changes.
The daily loop
In commands, one pass looks like this:
git pull origin main
sf project deploy preview --target-org my-scratch
sf project deploy start --target-org my-scratch
# work in the org or in your editor
sf project retrieve preview --target-org my-scratch
sf project retrieve start --target-org my-scratch
git add force-app
git commit -m "Add invoice validation"
git push origin feature/invoice-validationRun the preview commands first. They show conflicts before anything is overwritten.
Git operations count as local changes
Source tracking compares file contents on disk with its snapshot. It does not know about Git. When you pull, switch branches, or merge, files change on disk, and the CLI treats those differences as local changes.
This has two effects:
- After
git pull, the nextsf project deploy previewlists the files that teammates changed. Deploying them is usually what you want. - After switching to a branch that lacks a component you deployed earlier, the file looks deleted locally. A deploy would propose deleting it from the org if deletes are tracked.
Read the preview after every branch switch.
Handling conflicts and drift
Two kinds of conflict appear in a team setting. Git merge conflicts happen between branches and you resolve them in the files, as usual. Tracking conflicts happen between your files and your org, and you resolve them as described in the earlier chapter.
Do not reach for --ignore-conflicts as a shortcut after a merge. Preview first, decide which side is correct, then overwrite.
If you rebuild a scratch org, its tracking history starts fresh. If you change branches heavily and the histories no longer match, use sf project reset tracking --target-org my-scratch after you confirm the local files are what you want the org to match.
Team conventions
- Commit
.forceignoreso everyone excludes the same files. - Keep
.sfout of Git. It holds per-machine tracking state, and new projects already list it in.gitignore. - Merge to a shared branch only after retrieving and committing your org changes. Changes left in the org are invisible to Git.
- For shared sandboxes without tracking, deploy by manifest or metadata name from a reviewed branch, as covered in the untracked workflow chapter.
Exercise
You switch from your feature branch to main, and sf project deploy preview lists a class you created earlier as a local deletion. Why does it appear, and what should you do before deploying?
Show answer
The class exists in your scratch org and in the tracking snapshot but not in main's files, so the CLI sees it as deleted locally. Read the preview. If you only switched branches to look at something, switch back instead of deploying. Deploy only if you really want the class removed from the org.