[SalesForce] How to sync and overwrite your scratch org classes back to your local SFDX repo

I am facing an issue, where I accidentally corrupted my local SFDX repository. But fortunately, I have already pushed the changes to the SFDX scratch org.
Now the issue is my scratch org has the actual good code, but on my local machine the code is corrupted, and I can't pull it back from the scratch org instance.

when I do

sfdx force:source:pull -f

It says no results found.

Similarly to check the syncing I moved a class out of the local repository, and did the pull command, with the expectation that SF will detect the missing class file and generate a copy from the scratch org.
None of this happened.

Please help.

Best Answer

DX is still a work in progress; it's production quality but definitely has some known limitations. I do recall that this limitation is one that the DX team is looking to address, but that's not a current feature, so we can't depend on that.

This basically happens today because Salesforce doesn't do a full sync when pulling, but only tracks things that have changed on the server since the last push. Since there are no changes, it won't pull anything. In the future, a cleaner alternative might become available (hopefully sooner than later!).

Hopefully, you already have a package you can use in your scratch org to pull everything back down? If you do, it's pretty much as simple as:

sfdx force:mdapi:retrieve -p "Some Package" -d . -u userNameOrAlias
unzip unpackaged
sfdx force:mdapi:convert -r "Some Package"

This assumes you have a project configuration file, want to use the default path, and just looking to put everything back in order. You'll have to do some clean up and diffs afterwards, but at least you'll be back in sync.

Otherwise, if you don't have a package, you'll have to build one, or a manual package.xml file to download everything you want. If you do that, the force:mdapi:retrieve command uses -k name-of-package.xml instead of the -p "Package Name" format.

Related Topic