For daily Salesforce development you need about a dozen sf CLI commands. Use sf org login web to authenticate, sf project generate and sf project retrieve start to create and pull source, and sf project deploy start (with validate and quick) to deploy. Add sf apex run test for tests and sf org create scratch for scratch orgs. These are the current sf (v2) versions of the old sfdx force:* commands. You can look anything else up with sf <command> --help when you need it.
Below are the commands grouped by task, each with the flags you'll use most and the old sfdx syntax it replaces.
Install and check the CLI
The current CLI is distributed as the @salesforce/cli package. If you still have the old sfdx-cli npm package installed, uninstall it first so the two don't clash on your PATH.
npm uninstall sfdx-cli --global
npm install @salesforce/cli --global
sf version # confirm install
sf update # update an installer-based install
sf commands # list every available command
sf search # interactive command search
sf project deploy start --help # full flag reference for one command
sf search helps a lot while you're learning the new names. Type "deploy" and it shows every matching command.
sfdx vs sf: what changed
Salesforce merged the two CLIs into sf (v2). The sfdx executable is still there as an alias, so old scripts using sfdx force:... keep working for now. But the force:source and force:mdapi command families are deprecated, and new features only arrive in sf-style commands.
Three things changed:
- Command structure. Commands follow a
topic verbpattern (sf org create scratch) instead of colon-separated namespaces (force:org:create). - Flag names. Flags are kebab-case and consistent across commands.
-u/--targetusernameis now--target-org(-o),--targetdevhubusernameis now--target-dev-hub, and--sourcepathis now--source-dir. - Source tracking. Push and pull are gone.
sf project deploy startandsf project retrieve starthandle both tracked orgs (scratch orgs, source-tracked sandboxes) and untracked orgs.
| Task | Old sfdx command | Current sf command |
|---|---|---|
| Log in via browser | sfdx force:auth:web:login | sf org login web |
| Log in via JWT (CI) | sfdx force:auth:jwt:grant | sf org login jwt |
| Set default org | sfdx force:config:set defaultusername=x | sf config set target-org=x |
| Create project | sfdx force:project:create | sf project generate |
| Create Apex class | sfdx force:apex:class:create | sf apex generate class |
| Create LWC | sfdx force:lightning:component:create --type lwc | sf lightning generate component --type lwc |
| Retrieve source | sfdx force:source:retrieve / force:source:pull | sf project retrieve start |
| Deploy source | sfdx force:source:deploy / force:source:push | sf project deploy start |
| Validate only | sfdx force:source:deploy --checkonly | sf project deploy validate |
| Deploy mdapi format | sfdx force:mdapi:deploy | sf project deploy start --metadata-dir |
| Run Apex tests | sfdx force:apex:test:run | sf apex run test |
| Anonymous Apex | sfdx force:apex:execute | sf apex run |
| SOQL query | sfdx force:data:soql:query | sf data query |
| Create scratch org | sfdx force:org:create | sf org create scratch |
| Delete scratch org | sfdx force:org:delete | sf org delete scratch |
If you maintain CI scripts, rewrite them now. Don't wait for the deprecated commands to be removed.
1. Authenticating to orgs
Every other command needs an authenticated org. Give each org an alias you'll remember, because you'll type it all day.
# Production or Developer Edition, set as Dev Hub
sf org login web --alias devhub --set-default-dev-hub
# Sandbox (use test.salesforce.com or your My Domain URL)
sf org login web --alias uat --instance-url https://test.salesforce.com --set-default
# CI/CD: headless login with a connected app and a private key
sf org login jwt --username ci@acme.com --jwt-key-file server.key \
--client-id 3MVG9... --alias ci-prod --instance-url https://login.salesforce.com
Commands for managing the orgs you've authorised:
sf org list # all authorised orgs and scratch orgs
sf org display --target-org uat # username, instance URL, API version
sf org open --target-org uat # open the org in a browser
sf org open --target-org uat --path lightning/setup/ApexClasses/home
sf config set target-org=uat # default org for this project
sf org logout --target-org uat --no-prompt
sf config set writes to .sf/config.json inside the project, so each project can have its own default org. Add --global to set it machine-wide. Once you've set a default, you can leave --target-org off most commands. In scripts, pass it explicitly anyway, so nobody deploys to the wrong org by accident.
JWT login needs a connected app with a certificate uploaded and the user pre-authorised. That setup is the same connected-app and JWT work you'd do for an API integration.
2. Creating and retrieving source
Create the DX project
sf project generate --name acme-crm --manifest
cd acme-crm
git init
This creates sfdx-project.json, a force-app/main/default folder, a config/project-scratch-def.json, a .forceignore, and (because of --manifest) a starter manifest/package.xml.
Generate metadata locally
sf apex generate class --name AccountService --output-dir force-app/main/default/classes
sf apex generate trigger --name AccountTrigger --sobject Account --output-dir force-app/main/default/triggers
sf lightning generate component --name accountCard --type lwc --output-dir force-app/main/default/lwc
Leave out --type lwc and you get an Aura component, which is the default.
Retrieve from an existing org
Most developers start from an org that already has metadata. You can retrieve it in three ways:
# Specific metadata by type and name
sf project retrieve start --metadata ApexClass:AccountService --metadata CustomObject:Invoice__c --target-org uat
# Everything listed in a manifest
sf project retrieve start --manifest manifest/package.xml --target-org uat
# Refresh what's already in a local folder
sf project retrieve start --source-dir force-app/main/default/lwc --target-org uat
To build a manifest from what's in an org, generate it first and trim it down:
sf project generate manifest --from-org uat --output-dir manifest --name full-org
In a source-tracked org (a scratch org, or a sandbox with tracking enabled), run sf project retrieve start with no flags to pull only what changed in the org since your last sync. That's the replacement for force:source:pull. Run sf project retrieve preview first if you want to see what's coming down.
3. Deploying metadata
sf project deploy start is the command you'll run most. It accepts the same --source-dir, --metadata and --manifest flags as retrieve.
# Deploy a folder to a sandbox, no tests
sf project deploy start --source-dir force-app --target-org uat
# Deploy one class and run its test class
sf project deploy start --metadata ApexClass:AccountService \
--test-level RunSpecifiedTests --tests AccountServiceTest --target-org uat
# Dry run: validate and run tests without saving anything
sf project deploy start --source-dir force-app --test-level RunLocalTests --dry-run --target-org prod
In a tracked org, sf project deploy start with no flags deploys only your local changes. That replaces force:source:push. Use sf project deploy preview to see what would deploy and to spot conflicts.
Validate now, deploy later
For production releases, validate during the day and quick-deploy in the release window:
sf project deploy validate --source-dir force-app --test-level RunLocalTests --target-org prod
# returns a job ID such as 0Af...
sf project deploy quick --job-id 0Af5g00000XXXXX --target-org prod
A quick deploy skips re-running tests because the validation already ran them. Salesforce only allows it for a limited time after a successful validation (10 days).
Other deploy commands you'll use when a deploy is slow or your terminal drops:
sf project deploy report --job-id 0Af... # check status
sf project deploy resume --job-id 0Af... # reattach and wait
sf project deploy cancel --job-id 0Af...
Test levels
--test-level | What runs | When to use |
|---|---|---|
NoTestRun | Nothing | Sandbox deploys (not allowed for Apex in production) |
RunSpecifiedTests | Only the classes in --tests | Targeted production deploys; each deployed class needs coverage from those tests |
RunLocalTests | All tests except managed-package tests | Default choice for full production releases |
RunAllTestsInOrg | Everything, including managed packages | Rarely needed |
4. Running Apex tests
# One class, synchronous, readable output with coverage
sf apex run test --class-names AccountServiceTest --synchronous \
--result-format human --code-coverage --target-org uat
# Specific methods
sf apex run test --tests AccountServiceTest.testBulkInsert --result-format human --target-org uat
# All local tests, wait up to 10 minutes, write JUnit for CI
sf apex run test --test-level RunLocalTests --wait 10 \
--result-format junit --output-dir test-results --code-coverage --target-org uat
If you leave out --wait or --synchronous, the command returns a test run ID straight away. Fetch the results later:
sf apex get test --test-run-id 707XXXXXXXXXXXX --code-coverage --result-format human
Commands you'll reach for while debugging tests:
sf apex run --file scripts/apex/seed.apex --target-org uat # anonymous Apex
sf apex tail log --color --target-org uat # stream debug logs live
sf apex list log --target-org uat
sf data query --query "SELECT Id, Name FROM Account LIMIT 5" --target-org uat
Hitting 75% coverage is the deploy gate, but coverage alone doesn't prove the tests are good. Async tests, System.runAs, the Assert class and callout mocks are where most test suites are weak. Our article on Apex test classes beyond 75% coverage explains what a useful test looks like.
5. Creating scratch orgs
Scratch orgs are short-lived orgs built from your project's definition file. You need an authorised Dev Hub, which you set up in section 1.
sf org create scratch --definition-file config/project-scratch-def.json \
--alias feature-invoice --duration-days 7 --set-default --target-dev-hub devhub
sf project deploy start # push the whole project into it
sf org assign permset --name Invoice_Admin # give yourself access
sf data import tree --plan data/sample-data-plan.json # load sample records
sf org generate password --target-org feature-invoice # if you need to log in from another browser
sf org open
Scratch orgs last 7 days by default and 30 days at most. Delete them when the feature is merged, because your Dev Hub has daily and active scratch org limits:
sf org delete scratch --target-org feature-invoice --no-prompt
A minimal project-scratch-def.json:
{
"orgName": "Acme Dev",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"settings": {
"lightningExperienceSettings": { "enableS1DesktopEnabled": true }
}
}
Where these commands fit in a source-driven workflow
In a source-driven workflow, the Git repository is the source of truth and orgs are disposable copies of it. The CLI commands move metadata between the two. A typical feature cycle:
- Branch.
git checkout -b feature/invoice-totals - Get an org.
sf org create scratch ...from the shared definition file, or use a personal sandbox with source tracking turned on. - Deploy the baseline.
sf project deploy startloads the current main branch into the org. - Build. Write code locally with
sf apex generate classandsf lightning generate component, and make declarative changes in Setup. - Sync.
sf project retrieve startbrings declarative changes down.sf project deploy startsends code changes up. Runsf project deploy previewwhenever you're unsure. - Test.
sf apex run test --test-level RunLocalTests --code-coverage - Commit and open a pull request.
git add force-app && git commit. Reviewers read real diffs of metadata files, not change set descriptions. - CI validates. The pipeline runs
sf org login jwt, thensf project deploy validate --test-level RunLocalTestsagainst the target org, and saves JUnit results. - Release. After merge,
sf project deploy quick --job-id ...or a fullsf project deploy startto production. - Clean up.
sf org delete scratch.
Two files decide how well this works. sfdx-project.json defines your package directories and source API version. .forceignore stops profiles, noisy settings and local files from being deployed or retrieved. Get them right on day one and most "it deployed things I didn't touch" problems disappear.
For where the CLI sits in the wider skill set (Apex, LWC, integration, testing), see the Salesforce developer roadmap.
Learning this with Salesforce Casts
A command reference won't show you how to fix a deploy error when it comes up against a real org. The Salesforce Casts catalogue includes Salesforce DX content alongside Admin, Flows, Apex, LWC, Aura, testing, integration and CPQ. Teja, an ex-engineer at Salesforce.com, builds every course live in a real org, line by line, with no slide decks and no copy-paste code.
Every course comes with a GitHub repo of its source code. Clone it, authenticate your own org, and run sf project deploy start to get the finished project in your environment, which exercises the same workflow described above.
Two courses pair well with the CLI work above:
- Apex Test Fest covers async tests,
System.runAs, theAssertclass and callout mocks. That's whatsf apex run testshould be running. - The Integration Master Course covers OAuth, connected apps and JWT, which is the setup behind
sf org login jwtin a CI pipeline.
A few limits, so you know what you're buying. Courses are streaming only. There are no live classes or 1:1 mentoring (questions go to the private discussion group). There are no refunds. Every plan unlocks every course, and the plans are listed on the pricing page.
FAQ
Do old sfdx commands still work?
Mostly, yes. sf (v2) includes an sfdx alias and still understands many sfdx force:* commands. But the force:source and force:mdapi families are deprecated and don't get new features, so new scripts and CI pipelines should use the sf-style commands and flags.
What replaced force:source:push and force:source:pull?
sf project deploy start and sf project retrieve start. When you run them with no source flags against a source-tracked org (a scratch org, or a sandbox with tracking enabled), they deploy or retrieve only the tracked changes, the way push and pull did. With --source-dir, --metadata or --manifest, they work against any org, including production.
Do I need a Dev Hub to use the sf CLI?
Only for scratch orgs and packaging. You can authenticate to sandboxes and production, then retrieve, deploy and run tests, without a Dev Hub. To create scratch orgs, enable Dev Hub in a production or Developer Edition org and log in with --set-default-dev-hub.
How do I deploy to production without running every test?
Use --test-level RunSpecifiedTests --tests YourTestClass. Salesforce still requires the deployed Apex classes and triggers to meet coverage from the tests you specify. For a full release, validate earlier with sf project deploy validate, then run sf project deploy quick in the release window so tests don't run twice.
Can I use these commands in GitHub Actions or another CI tool?
Yes. Install @salesforce/cli in the pipeline and authenticate with sf org login jwt (connected app plus private key stored as a secret). Then run sf project deploy validate or sf apex run test --result-format junit --output-dir test-results so the CI tool can show test results.
Does Salesforce Casts teach Salesforce DX and the CLI?
The catalogue includes DX content, and every course ships with a GitHub repo you can deploy to your own org with the CLI. Salesforce Casts doesn't drill certification exam questions or offer live mentoring. It teaches by building in a real org, and you'd pair it with Trailhead for exam prep.
Next step
Open a terminal and run sf org login web --alias devhub --set-default-dev-hub, then sf project generate --name practice --manifest, and work through sections 2 to 5 against a Developer Edition org. To see the same workflow on real projects, browse the course catalogue and clone the GitHub repo for whichever course you start.
Learn it by building it
Every course, every code repository, the discussion group and certificates of completion โ one subscription.