Sub topic 7
Controlling what is tracked with .forceignore
This chapter assumes you can read an sfdx-project.json file and know what a package directory is. It also assumes you have run sf project deploy start and sf project retrieve start at least once.
What .forceignore does
.forceignore is a text file in the project root, next to sfdx-project.json. Each line is a pattern. The CLI skips any file under your package directories that matches a pattern. The skip applies to three things:
- Deploys, so the file is never sent to the org.
- Retrieves, so the file is never written to your project.
- Source tracking, so changes to the file are not reported as local or remote changes.
The file does not affect Git. Use .gitignore to control what Git stores. Use .forceignore to control what the Salesforce CLI sends, writes, and tracks.
Pattern syntax
Patterns follow gitignore rules. The generated project includes a file like this:
# Ignore profile files and local tooling output
**/profiles/**
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**These are the rules you will use most often:
| Pattern | Matches |
|---|---|
| `# text` | A comment line. |
| `**/profiles/**` | Any `profiles` folder at any depth, and everything inside it. |
| `*.dup` | Any file ending in `.dup`. |
| `force-app/main/default/staticresources/legacy/**` | One specific folder, written from the project root. |
| `!keep-me.cls` | An exception that re-includes a file an earlier pattern matched. |
Order matters. A later pattern overrides an earlier one, so put exceptions after the rule they undo.
Checking your patterns
After you edit the file, preview a deploy before you trust it. A dry run validates the deploy without saving anything in the org:
sf project deploy start --dry-runThe output lists the components that would be deployed. A component you meant to exclude should not appear in that list. A component you meant to include should.
Common errors
- A file silently never deploys. A pattern is broader than you intended. For example,
**/*test*also matchesTestDataFactory.cls. Narrow the pattern or add a!exception below it. - A
!exception has no effect. As in Git, you cannot re-include a file when a parent folder is excluded. If you ignore**/legacy/**and then add!**/legacy/keep.cls, the exception fails. Ignore the files inside the folder with a more specific pattern instead. - The file is ignored but still appears in a retrieve list. The CLI reads
.forceignorefrom the project root only. A copy in a subfolder is not used. - Patterns fail on Windows. Write paths with forward slashes, not backslashes.
- An ignored file is still referenced. If a profile or other component refers to something you ignored, the deploy can fail in the org because the dependency is missing.
Keeping the file small
Add patterns for a reason you can name: local tooling output, metadata a team manages elsewhere, or files that differ per environment. A short file is easier to audit. When a deploy skips something unexpectedly, .forceignore is the first place to look.
Exercise
Your project contains the pattern **/profiles/**. You add !force-app/main/default/profiles/Admin.profile-meta.xml below it, but the Admin profile still does not deploy. Why, and how would you confirm the fix?
Show answer
The pattern **/profiles/** excludes the whole folder, and gitignore-style rules do not let you re-include a file when its parent folder is excluded. Replace the broad pattern with more specific ones that ignore only the profiles you do not want. Then run sf project deploy start --dry-run and check that the Admin profile appears in the component list.