Sub topic 5
Keeping files out of tracking with .forceignore
A Salesforce DX project holds more than metadata. It also has editor settings, test folders, lint configuration, and sometimes metadata you never want to move between orgs. The .forceignore file tells the Salesforce CLI to skip these files. This chapter covers how the file works, how its patterns behave, and how it interacts with source tracking.
This chapter assumes you have a working DX project with an authorized org. It also assumes you can run sf project deploy preview and read its output, and that you know basic Git ignore patterns.
What .forceignore does
.forceignore is a plain text file at the root of your project. Each line is a pattern. The CLI checks every file against these patterns during source operations. A file that matches is treated as if it did not exist for those operations.
The CLI consults .forceignore when you:
- deploy with
sf project deploy startorsf project deploy preview - retrieve with
sf project retrieve startorsf project retrieve preview - compute tracked changes in an org that supports source tracking
- convert source format with
sf project convert source
An ignored file never appears in the list of local changes. An ignored component in the org is never written to your project during a retrieve. Ignoring is therefore symmetric. It hides the file from both directions of synchronization.
The default file
When you create a project with sf project generate, the CLI writes a starter .forceignore. A typical one for a project with Lightning Web Components looks like this:
# List files or directories below to ignore them when running sf project deploy start or retrieve start
package.xml
# LWC configuration files
**/jsconfig.json
**/.eslintrc.json
# LWC Jest
**/__tests__/**Each entry has a reason:
jsconfig.jsonand.eslintrc.jsonare editor and lint settings. Salesforce has no use for them.__tests__folders hold Jest tests that sit inside LWC bundles. If they were deployed, the deploy would fail or the tests would land in the org as unwanted bundle files.package.xmlis a manifest you use to select components. It is not metadata that belongs in the org.
Pattern syntax
.forceignore follows the same pattern rules as .gitignore. The rules you will use most:
| Pattern | Meaning |
|---|---|
| `# text` | A comment. The whole line is skipped. |
| `**/name.json` | Any file called `name.json` in any directory. |
| `**/__tests__/**` | Everything inside any directory named `__tests__`. |
| `*.profile-meta.xml` | Any file whose name ends in `.profile-meta.xml`. |
| `force-app/main/default/profiles` | A specific directory, relative to the project root. |
| `!path/to/file` | A negation. It re-includes a file that an earlier pattern excluded. |
Use forward slashes in every pattern, including on Windows. Patterns are matched against paths relative to the project root.
Order and negation
Later lines override earlier ones. This lets you ignore a whole group and then make an exception:
# Ignore all profiles
**/profiles/**
# ...but keep the Admin profile
!**/profiles/Admin.profile-meta.xmlThis example hits a rule inherited from Git. You cannot re-include a file if a parent directory is already excluded. The pattern **/profiles/** excludes the files inside the directory, so the negation works. If you wrote **/profiles/ instead, the whole directory would be excluded and the ! line would have no effect. When a negation does nothing, check whether an earlier pattern excludes the parent folder.
How ignoring interacts with source tracking
Source tracking compares local files with org revisions. .forceignore runs before that comparison, so an ignored file is never a change.
This produces three behaviors that confuse people:
- Ignoring a file hides pending changes. If you edit a class and then add it to
.forceignore, it disappears fromsf project deploy preview. The edit is still on disk, but the CLI no longer reports it. - Ignoring does not delete from the org. If a component is already deployed and you add it to
.forceignore, it stays in the org. The CLI simply stops considering it. - Ignored org changes never arrive. If someone edits an ignored component in the org,
sf project retrieve previewwill not list it. It also cannot appear as a conflict.
You can check how the filter is affecting your project with the preview commands from the previous chapter:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchThe preview output separates files the CLI will act on from files it is skipping. Compare the lists before and after editing .forceignore to confirm that a pattern does what you meant.
Common uses
Keep tooling files out of the org
Editor, linter, and test files that live next to metadata are the standard case. Add them with **/ patterns so they match anywhere in the tree:
**/jsconfig.json
**/.eslintrc.json
**/.prettierrc
**/__tests__/**Skip metadata that differs between orgs
Some metadata should not move between environments. Examples include a profile that is configured differently in each org, or an integration setting with org-specific values. Ignoring these keeps a deploy from overwriting settings in the target org:
force-app/main/default/profiles
force-app/main/default/settings/Email.settings-meta.xmlBe careful with this approach. If a component is ignored, nothing in your project will ever update it. Teams that ignore a component for one environment should document that decision.
Exclude files you retrieve but never edit
A retrieve can pull in managed or standard components that you did not write and do not want to keep in Git. Ignoring them keeps the diff small and keeps the tracking list readable.
.forceignore and .gitignore are separate
The two files serve different tools, and a file often needs an entry in only one of them.
| Question | `.forceignore` | `.gitignore` |
|---|---|---|
| Who reads it? | Salesforce CLI | Git |
| What does it control? | What deploys, retrieves, and is tracked | What Git stages and commits |
| Commit it to Git? | Yes. The whole team needs the same rules. | Yes. |
| Typical content | `__tests__`, lint configs, environment-specific metadata | `.sf/`, `node_modules/`, `.sfdx/`, local scratch files |
The .sf directory holds your local tracking state, so it belongs in .gitignore, not in .forceignore. Tracking state describes your machine's relationship to one particular org. If it is committed, a teammate would inherit a state that does not match their own scratch org.
The reverse also holds. Do not ignore a metadata file in .forceignore just because you do not want to see it in Git. That would stop it from deploying as well.
Troubleshooting patterns
When a pattern behaves unexpectedly, work through these checks:
- The file is missing from the preview. Confirm that no earlier pattern matches it. Remember that a broad pattern can match more than you intended.
- A negation does not work. Check whether a parent directory is excluded. Rewrite the earlier pattern to match the contents (
dir/**), not the directory (dir/). - A pattern works on one machine and not another. Look for backslashes in paths, or for patterns that depend on a directory layout that differs between checkouts.
- Changes appear that you thought were ignored. The pattern may be anchored differently than you expected. A pattern with a slash in the middle is matched from the project root, so add
**/if you want it to match at any depth. - The file is ignored but still in the org. Ignoring never removes components from an org. Delete it with a destructive change if you want it gone.
Exercise
You edited force-app/main/default/classes/Billing.cls, then added **/Billing.cls to .forceignore. You run sf project deploy preview against your scratch org. What do you expect to see, what happens to the copy of Billing already in the org, and how would you confirm that a later change to the pattern fixes it?
Show answer
The preview no longer lists Billing.cls as a local change, because .forceignore filters files before the CLI compares them with tracking state. The edit stays on disk but is not reported, and it will not deploy. The copy of Billing already in the org is untouched, because ignoring a file does not delete it from the org. To confirm a fix, remove or narrow the pattern, then run sf project deploy preview --target-org my-scratch again and check that Billing.cls appears in the list of changes to deploy.