Sub topic 2
Laying out a Salesforce DX project in Git
This chapter assumes you know basic Git (commit, branch, merge, pull request), can read Salesforce metadata such as Apex classes, objects, and flows, and can use a command line. It covers what goes into the repository and how the project file tells the Salesforce CLI where to find it.
What belongs in Git
In a Salesforce DX project, the repository holds source format metadata. Source format splits large metadata types into small files. For example, a custom object becomes a folder with one file per field. Smaller files mean that two developers who edit different fields touch different files, so merges are less likely to conflict.
Git should track:
- Apex classes, triggers, and tests
- Lightning web components
- Objects, fields, and validation rules
- Flows, permission sets, and page layouts
- Project configuration, such as
sfdx-project.jsonand scratch org definitions
Git should not track secrets, auth URLs, or files the CLI generates for your local machine.
The project file
The file sfdx-project.json sits at the repository root. It tells the Salesforce CLI which directories hold source and which API version to use.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}packageDirectorieslists the folders the CLI deploys from and retrieves into. Exactly one entry is markeddefault, and retrieved files land there.namespacestays empty unless you build a namespaced managed package.sfdcLoginUrlis the login endpoint used when you authorize an org.sourceApiVersionsets the metadata API version for deploys and retrieves.
Everything under force-app is what your pull requests will show. Changes outside the listed directories are invisible to the CLI.
A typical layout
The config folder holds scratch org definitions. The force-app/main/default folder holds the metadata, with one subfolder per metadata type.
Two ignore files
Two files with similar names do different jobs.
| File | Read by | Purpose |
|---|---|---|
| `.gitignore` | Git | Keeps local and generated files out of the repository |
| `.forceignore` | Salesforce CLI | Keeps metadata out of deploys and retrieves |
A typical .gitignore includes .sfdx/, .sf/, node_modules/, and local editor settings. A typical .forceignore excludes files such as **/jsconfig.json and **/.eslintrc.json. It can also exclude metadata that differs by org and causes noisy diffs.
Common errors
- Retrieved files land in the wrong place. The
defaultflag is missing or points at the wrong directory. CheckpackageDirectories. - Deploy says "no source-backed components". The files sit outside every listed
path. Move them underforce-appor add the directory to the project file. - Huge diffs after a retrieve. The repository mixes API versions or the retrieve pulled in environment-specific metadata. Pin
sourceApiVersionand add noisy items to.forceignore. - Local CLI state in commits. The
.sf/or.sfdx/folder was not ignored.
Putting it together
Create the project, commit the layout, and push it. Every branch in the strategy that follows starts from this structure.
sf project generate --name my-project
cd my-project
git init -b main
git add .
git commit -m "Initial Salesforce DX project"Exercise
A developer retrieves a flow from a sandbox, but the file does not appear under force-app. Name two things in sfdx-project.json you would check.
Show answer
Check that packageDirectories contains a path entry marked "default": true, because retrieved files land in the default directory. Also check that the path points at the folder you expect, such as force-app. If both are right, check .forceignore for a pattern that excludes the file.