Sub topic 2
Laying out a Salesforce DX project in Git
This chapter assumes you can commit, branch, and merge in Git, and that you know common metadata types such as Apex classes, custom objects, and flows. It also assumes you can run commands in a terminal. By the end, you will know which files belong in the repository, which do not, and how the project layout affects branching later.
What the repository represents
A Salesforce DX project is a directory of files that the Salesforce CLI can deploy to an org and retrieve from one. In the model this tutorial uses, the Git repository is the source of truth. An org is a place where that source runs. It is not the place where the source lives.
The layout matters for branching because Git merges files, not org state. If two developers change the same file, Git can only merge it well when the file is split sensibly and written in a predictable order. The DX source format is designed for this.
Source format and metadata format
Salesforce metadata can be stored in two shapes.
- Metadata format is the older Metadata API layout. A custom object is one large
.objectXML file that contains every field, validation rule, and list view. Apackage.xmlmanifest lists what to deploy. - Source format is the DX layout. The same object is a folder, and each field, validation rule, and list view is its own small XML file.
Source format produces smaller diffs and fewer merge conflicts. Two developers who each add a field to Case touch two different files, so Git merges them without help. In metadata format both developers would edit the same large file.
Keep the repository in source format. Convert to metadata format only when a tool requires it, for example with sf project convert source --root-dir force-app --output-dir mdapi-output.
The directory layout
Create a project with the CLI:
sf project generate --name salesforce-app --template standardThe result looks like this, trimmed to the parts that matter:
salesforce-app/
├── .forceignore
├── .gitignore
├── sfdx-project.json
├── config/
│ └── project-scratch-def.json
├── manifest/
│ └── package.xml
├── scripts/
│ └── apex/
└── force-app/
└── main/
└── default/
├── classes/
├── triggers/
├── objects/
│ └── Case/
│ ├── fields/
│ └── validationRules/
├── flows/
├── lwc/
├── permissionsets/
└── layouts/Each top-level item has one job:
| Path | Purpose | Tracked in Git |
|---|---|---|
| `sfdx-project.json` | Declares package directories, API version, and login URL | Yes |
| `force-app/` | The metadata that gets deployed | Yes |
| `config/project-scratch-def.json` | Shape of scratch orgs created for this project | Yes |
| `manifest/package.xml` | Optional list of metadata to retrieve or deploy | Yes |
| `scripts/` | Anonymous Apex and data scripts for developers | Yes |
| `.forceignore` | Metadata the CLI must skip | Yes |
| `.sf/` and `.sfdx/` | Local CLI cache and org state | No |
| `node_modules/` | Tooling dependencies | No |
sfdx-project.json
This file tells the CLI which directories hold deployable source. Here is a minimal version:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Each field has a specific effect:
packageDirectorieslists folders the CLI treats as source. Anything outside them is ignored by deploy and retrieve.default: truemarks where newly retrieved metadata lands when the CLI cannot tell where it belongs.namespacestays empty unless you build a namespaced managed package.sfdcLoginUrlis the login host. Change it tohttps://test.salesforce.comonly if you authenticate to sandboxes through this file's default.sourceApiVersionsets the API version used for deploys and retrieves.
Treat sourceApiVersion as a team decision. If one developer bumps it in a feature branch, every later deploy uses the new version, and the change appears in every pull request that touches the file. Change it in its own pull request.
Several package directories
A larger project can declare more than one directory:
{
"packageDirectories": [
{ "path": "force-app", "default": true },
{ "path": "unpackaged-config" }
],
"sourceApiVersion": "62.0"
}Two rules apply. Exactly one directory has default: true. The same metadata component must not appear in two directories, or the CLI reports a duplicate. Later chapters cover when splitting a project into modules is worth the cost. A single force-app directory is the right start for most teams.
What to commit and what to ignore
Two ignore files do different jobs, and mixing them up is a common source of confusion.
.gitignoretells Git which files never enter the repository..forceignoretells the Salesforce CLI which source files to skip when deploying and retrieving. Files listed there can still be in Git.
A starting .gitignore:
# Salesforce CLI local state
.sf/
.sfdx/
.localdevserver/
# Dependencies and logs
node_modules/
*.log
# Credentials and local secrets
auth-url.txt
*.key
.env
# Editor files
.idea/
.vscode/*
!.vscode/extensions.json
!.vscode/settings.jsonA starting .forceignore:
# Local tooling config that is not Salesforce metadata
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
# Metadata the team chooses not to manage in Git
**/profiles/**
**/settings/Social.settings-meta.xmlThe profile entry is an example of a team decision, not a default. Profiles are large, order-sensitive XML files and a frequent cause of conflicts. Some teams ignore them and manage access with permission sets. Others keep them and accept the cost. Decide early and write it down, because adding profiles to the ignore list later leaves stale files in older branches.
Never commit auth URLs, access tokens, or JWT keys. The CI example later in this tutorial reads the login URL from a stored secret for that reason.
How the layout shows up in diffs
The file system structure controls what a pull request looks like. A change that adds one field and edits one Apex class touches these paths:
force-app/main/default/objects/Case/fields/Routing_Priority__c.field-meta.xml
force-app/main/default/classes/CaseRouter.cls
force-app/main/default/classes/CaseRouter.cls-meta.xmlEvery Apex class and trigger has a companion -meta.xml file that holds its API version and status. Commit both files together. A class without its meta file fails to deploy.
Common errors
- Retrieve writes files outside the expected folder. The CLI places new components in the default package directory. Check
default: trueand thepathvalues. - "Duplicate component" or similar on deploy. The same component exists in two package directories. Remove one copy.
- A deploy ignores a file that is in Git. A pattern in
.forceignorematches it. Test patterns against the exact relative path. - Every retrieve changes dozens of files. The org and the project use different API versions, or the retrieve rewrote formatting. Align
sourceApiVersionand commit retrieved changes in a separate commit. - Local
.sffiles appear as untracked changes. They are missing from.gitignore.
Summary of decisions to make now
Settle these before the first feature branch, because changing them later rewrites many files at once:
- Which API version the project targets.
- Whether profiles are tracked or ignored.
- Whether the project uses one package directory or several.
- Which developer-local files go in
.gitignore.
Exercise
A developer retrieves a custom object from a sandbox and sees one large file for the whole object, including all fields, instead of one file per field. Which format is the repository using for this component, and why do they get more merge conflicts than a team using the other format?
Show answer
The component is in metadata format, where one .object file holds every field, rule, and list view. Two developers who each add a field edit the same file, so Git often reports a conflict. In source format each field is its own file under objects/<Name>/fields/, so those two changes touch different files and merge cleanly. Keeping the repository in source format avoids the problem.
Exercise
Your team lists **/profiles/** in .gitignore instead of .forceignore. A developer still sees profile changes land in the org after a deploy from a fresh clone. What is the likely reason, and which file should hold that rule?
Show answer
.gitignore only controls what enters Git. If profile files are not committed, a fresh clone has none, so those profile changes cannot come from the clone. If profiles do deploy, they are committed, so the .gitignore rule is not working, for instance because the files were already tracked before the rule was added. To stop the CLI from deploying or retrieving profiles, the rule belongs in .forceignore. Files already tracked also need to be removed from Git with git rm --cached.