Simple way to diff a file in local SFDX project against Production version with SFDX

Salesforce Asked by Brian Miller on January 4, 2022

Although we use git repos to track all our projects, I oftentimes want to do another check against a production file before I overwrite it with an sfdx force:source:deploy command (because as much as a git repo is kept up-to-date, you never know what renegade changes have been made to Production…)

Is there a simple sfdx workflow or command to bring in that file and compare against my local project version? Context: I use VS Code and git for day-to-day development.

3 Answers

After installing the sfdx:source:diff tool from @Mohith Shrivastava's response, you can use the --json flag to return the locations of the remote and local files that VS Code uses to diff. You can then pass those files to git diff to see the diff in the command line.

Here is the whole command:

SFDX_DIFF_JSON=$(sfdx force:source:diff -p force-app/main/default/classes/MyController.cls -u MyProductionOrg --json)
git diff $(echo ${SFDX_DIFF_JSON} | jq '.result.remote' -r) $(echo ${SFDX_DIFF_JSON} | jq '.result.local' -r)

Answered by bbbco on January 4, 2022

Have you tried using the Salesforce official Diff Feature on the Extension Pack for Salesforce

From the terminal, run sfdx plugins:install @salesforce/sfdx-diff.

After the installation is complete, @salesforce/sfdx-diff appears in the list of installed plugins when running sfdx plugins.

Use the vscode menu to take the diff

enter image description here

This requires enabling the preview flag from your vscode settings

enter image description here

Answered by Mohith Shrivastava on January 4, 2022

VS Code has an option to detect conflicts on deployment for non-source-tracked orgs. Go to Settings > Salesforce Feature Previews > Salesforcedx-vscode-core: Detect Conflicts At Sync. Note that this is still a limited feature, so some caution is advised.

In the interim, if I want to check for differences, I usually do:

git checkout -b temp-branch
git add force-app
git commit -m "temp commit for compare"
sfdx force:source:retrieve -p force-app
git diff

This gives me a line-by-line analysis of everything. I can also use --name-only just to get a feel for which files are affected.

Answered by sfdcfox on January 4, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP