Sub topic 14
Combining source tracking with Git in a team
This chapter assumes you know how to preview, deploy, and retrieve tracked changes, how conflicts arise, and what .forceignore does. It also assumes you can use basic Git commands: clone, branch, commit, merge, and pull. The chapter covers how Git and source tracking divide the work, what to commit, and how to keep each developer's org in step with the shared branch.
Two systems, two jobs
Git and source tracking solve different problems, and they do not share state.
- Git records the history of your project files and lets a team merge work. It is the source of truth for what the project should contain.
- Source tracking records the relationship between one local project and one org. It tells the CLI which components differ, so a deploy or retrieve moves only those components.
Source tracking knows nothing about branches, commits, or teammates. It only compares your working files with a snapshot of what the org last saw. Git knows nothing about the org. A team workflow works when you keep these roles separate. Git decides what the code should be. Tracking helps each developer move that code into their own org and capture changes made there.
What to commit and what to keep local
Tracking state lives in the .sf directory of your project (older projects may have .sfdx). It holds the local snapshot for each org you have authorized. That snapshot is tied to one machine, one project folder, and one org ID. Another developer cannot use it, so it must not be shared.
New projects created with the CLI include a .gitignore that already excludes these directories. Check that yours does:
.sf/
.sfdx/
.localdevserver/
node_modules/| Item | Commit to Git? | Why |
|---|---|---|
| `force-app/` source files | Yes | This is the project itself. |
| `sfdx-project.json` | Yes | Defines package directories and API version for everyone. |
| `.forceignore` | Yes | Everyone should skip the same files. |
| `config/project-scratch-def.json` | Yes | Every developer creates matching scratch orgs. |
| `manifest/package.xml` | Yes, if you use it | Lets CI deploy to orgs without tracking. |
| `.sf/` and `.sfdx/` | No | Per-machine, per-org snapshot state. |
| Org auth files and aliases | No | They contain credentials and are personal. |
Committing .sf causes two problems. A teammate who pulls it gets a snapshot that describes your org, not theirs, so the CLI may skip components that need to deploy. It also creates merge conflicts in files nobody should edit by hand.
One org per developer
Tracking works best when one person works in one org. A scratch org supports tracking, and each developer can create one from the shared definition file:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultBecause each org has a single writer, org-side changes come only from that developer. Conflicts, which happen when the same component changes both locally and in the org, become rare. A shared org with several people clicking in Setup produces conflicts all the time, because every person's changes show up as org changes in everyone else's tracking.
A day-to-day loop
A typical feature branch follows this order.
- Update your branch from the shared branch with Git.
- Preview what the org would receive, then deploy it.
- Build and test in the org, using code or Setup.
- Preview what the org changed, then retrieve it.
- Review the diff with Git, commit, and push.
In commands:
git checkout -b feature/invoice-totals
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 status
git add force-app
git commit -m "Add invoice totals trigger handler"
git push origin feature/invoice-totalsRun git status and git diff after each retrieve. The retrieve writes org changes into your working tree, and Git is where you decide which of those changes belong in the commit. Setup edits sometimes bring along unrelated metadata, such as a profile that picked up extra permissions. Review those lines before committing them.
How Git operations look to tracking
Tracking compares file contents with its snapshot. It does not care how a file changed, so Git operations count as local edits.
- After
git pullorgit merge, files that came from teammates appear as local changes. The nextdeploy startsends them to your org. This is usually what you want. - After
git checkoutto another branch, files that differ between the branches appear as local changes. Files that exist only on the old branch appear as local deletions. - After
git stash, the stashed edits disappear from the working tree and are treated like a revert. Applying the stash brings them back as changes.
Deletions deserve care. In a tracked org, a file removed locally is a pending delete, and a deploy removes the component from the org. Switching to a branch that lacks a class therefore removes that class from your scratch org on the next deploy. That is fine in a disposable org, but it can surprise you if the component holds test data or setup you rely on. Run the preview first:
git checkout release/2026-winter
sf project deploy preview --target-org my-scratchRead the output for deletes before you run deploy start.
Conflicts after a merge
A conflict needs the same component changed both locally and in the org since the last sync. In a team, the usual cause is this sequence: you edit a class in your org, a teammate edits the same class on main, and you pull. Your local file now differs from the snapshot, and the org copy differs from the snapshot too.
Resolve it in this order:
- Run
sf project retrieve previewandsf project deploy previewto see which components are in conflict. - Decide which version is correct. Git is the place to merge, so the usual answer is to bring the org version into your working tree first.
- Retrieve the org change with
--ignore-conflicts, so the org version overwrites the local file. Git still holds your pre-retrieve version, so you can compare and merge by hand. - Fix the file, commit, and deploy.
sf project retrieve preview --target-org my-scratch
sf project retrieve start --target-org my-scratch --ignore-conflicts
git diff force-app/main/default/classes/InvoiceService.clsCommit or stash your local edits before an --ignore-conflicts retrieve. Otherwise the retrieve overwrites uncommitted work, and Git cannot restore it.
When the snapshot and the project drift apart
Some Git actions leave the snapshot describing something that no longer matches your files. Examples are rewriting history, restoring a project from a backup, or copying a project folder to a new path. If previews show many unexpected changes or conflicts, reset the tracking state for that org:
sf project reset tracking --target-org my-scratchReset tells the CLI to treat the org and your local files as synchronized at the revision you choose. It does not move any files. Use it only when you have confirmed that the org and your working tree actually match, or the next deploy and retrieve will miss real differences.
For a new teammate, nothing carries over. A fresh clone has no .sf state. Their first deploy to a new scratch org sends the whole project, and tracking starts from that point.
Shared orgs without tracking
Integration sandboxes, UAT orgs, and production usually have no tracking. The CLI cannot compute changes there, so the pipeline must say what to deploy. Run it from a CI job on the shared branch, using the selection methods from the previous chapter:
sf project deploy start --target-org my-sandbox --manifest manifest/package.xml --dry-run
sf project deploy start --target-org my-sandbox --manifest manifest/package.xmlThe first command validates without saving. The second deploys. Keep the manifest in Git so that a reviewer can see in a pull request which components a release includes. Some teams generate the list from git diff between two commits, either with a script or a third-party plugin. If you do, review the generated list, because renamed and deleted files need special handling.
Team rules that prevent most problems
- One developer per scratch org. Treat a shared org as a deployment target, not a workspace.
- Never commit
.sfor.sfdx. - Preview before every deploy that follows a branch switch or merge.
- Commit before any retrieve that uses
--ignore-conflicts. - Make Git the final authority. If the org and the main branch disagree, the branch wins unless the team decides otherwise.
- Share one
.forceignore, so no one commits or deploys files others have excluded.
Exercise
A teammate sends you their .sf directory so that "your deploys will be fast." Why should you not copy it into your project, and what should you do instead?
Show answer
The .sf state is a snapshot tied to the teammate's org, machine, and project path. Copying it makes the CLI compare your files with a snapshot of a different org, so it can skip components that your org needs or report false conflicts. Keep .sf in .gitignore, create your own scratch org, and let your first deploy build your own tracking state.
Exercise
You run git checkout to switch to a branch where InvoiceService.cls does not exist. Before deploying to your scratch org, what should you run, and what will you expect to see?
Show answer
Run sf project deploy preview --target-org my-scratch. Because the file is missing locally, the preview should list InvoiceService as a pending delete. Deploying would remove the class from your scratch org, so confirm that is what you want before running deploy start.
Exercise
After pulling main, the previews show a conflict on one Apex class that you also edited in your org. Give the safe order of steps to resolve it.
Show answer
Commit or stash your local edits first. Run both previews to confirm which components conflict. Retrieve with --ignore-conflicts so the org version lands in your working tree. Use git diff to compare it with the merged version, fix the file by hand, commit, and then deploy.