How to forceignore a whole folder, except a specific file in your DX Project

⏱ 2 min. read

Intro to the issue

Have you ever worked with scratch orgs and wondered how you could ignore all changes to a specific folder (e.g Profiles), except to just one specific file in that folder, let’s say System Admin profile?

The first thing that comes into your mind is copy-pasting all the file paths that you want to ignore, except the one that you want to NOT ignore. This would result on having a lot of lines, making the .forceignore file a lot longer and not so easily readable.

Don’t rush and do that, because there is a better solution to it 🎉

The Solution

We can use the exclamation point “!” to make the specific exclusion, just like we would use it in our validation rules, apex or in a .gitignore file.

⬇️ Below is the example snippet you could copy into your .forceignore file

1
2
force-app/main/default/profiles/**
!**/Admin.profile-meta.xml

So, above we are ignoring every change in every profile, except to the System Admin one.

Example to try out

  1. Add the above snippet to your .forceignore file
  2. Create and open a fresh scratch org
  3. Go to profiles and modify the System Admin and also the Standard User profiles.
  4. Save the changes.
  5. Jump to vscode and pull the changes from your scratch org with:
    1
    sfdx force:source:pull -f 
  6. You will notice that the CLI will only pull the changes to the System Admin profile 🙌🏼

You can of course use this example with all other folders and files in your DX project with just a few simple adjusments.

Sources

example

1️⃣ Salesforce DX Developer Guide
2️⃣ Salesforce Stackexchange
3️⃣ salesforce cli issue

Disclaimer

The articles posted in this blog assume that you already have a deep developer knowledge. This is why I do not explain things more thoroughly, but just give out the solution.