Sub topic 6
Aliases and the default target org
Almost every sf command that touches an org needs to know which org to act on. Typing a full username such as [email protected] each time is slow and error-prone. This chapter covers the two features that remove that friction: aliases and the default target org. It also covers how the CLI decides which org a command uses when several settings disagree.
This chapter assumes you can run sf commands in a terminal, have installed the CLI, and have already authorized at least one org with sf org login web. It also assumes you know that a Salesforce username is unique across all orgs.
What the CLI stores about an org
When you authorize an org, the CLI saves the authorization details on your machine. These include the username, the org ID, the instance URL, and a refresh token. A username is the unique identifier for that authorization. Every command that targets an org ultimately resolves to one of these saved usernames.
Two separate ideas sit on top of this:
- An alias is a short nickname that maps to a username.
- The default target org is a configuration value that tells commands which org to use when you do not name one.
They are independent. An alias does nothing unless you use it, and a default can be set with an alias or with a raw username.
Aliases
Setting an alias at login
The easiest way to create an alias is at authorization time with --alias (short form -a):
sf org login web --alias myorg --set-default
sf org listAfter login, sf org list shows the alias next to the username. From then on, myorg and the full username refer to the same org.
Managing aliases directly
The alias topic manages the nickname list without logging in again:
sf alias set [email protected]
sf alias set [email protected]
sf alias list
sf alias unset dev1Notes on how aliases behave:
sf alias set name=valuecreates the alias, or overwrites it if the name already exists. There is no confirmation prompt.- An alias can point to any value you give it. The CLI does not check that the username is authorized when you create the alias. A typo shows up later as an error when you use it.
- Aliases are stored per user on your machine, not per project. The same alias works in every directory.
- Alias names are case-sensitive and should avoid spaces and characters your shell treats specially.
Aliases and scratch orgs
Scratch orgs get usernames you did not choose, so aliases matter more there. You pass --alias when creating the org, which a later chapter covers. Scratch orgs expire, so aliases for expired orgs can pile up. Remove stale ones with sf alias unset, or delete the org with the org commands, which also clears its authorization.
The default target org
Setting it
You can set the default at login with --set-default, as shown above. You can also set it later with the configuration commands:
sf config set target-org myorg
sf org displaytarget-org is the configuration key. Its value can be an alias or a username. sf org display with no flags shows details about the org the CLI currently treats as the default, including its username, org ID, instance URL, and alias.
Using it
Once a default is set, commands that need an org work without any org flag:
sf data query --query "SELECT Id, Name FROM Account LIMIT 5"
sf project deploy start --source-dir force-app
sf apex run --file hello.apexTo override the default for a single command, pass --target-org (short form -o):
sf data query --query "SELECT COUNT() FROM Account" --target-org prod
sf project retrieve start --source-dir force-app -o dev1This is the safest way to work against more than one org. The flag affects only that command and leaves your configuration unchanged.
Local and global scope
Configuration has two scopes:
| Scope | Where it is stored | When it applies |
|---|---|---|
| Local | `.sf/config.json` inside your project folder | Only when you run commands from inside that project |
| Global | `~/.sf/config.json` in your home directory | Anywhere, unless a local value overrides it |
By default, sf config set writes the local scope, and it must be run inside a Salesforce DX project. Add --global to write the global file instead:
sf config set target-org myorg --globalA local value wins over a global one. This lets you keep a personal default for general use and a different default inside each project. The project's local file is an ordinary JSON file, so you can read it:
{
"target-org": "myorg"
}Do not commit the .sf folder to version control. A new DX project's .gitignore already excludes it, and a default org alias in a shared repository would point teammates at an org they have not authorized.
Inspecting and removing configuration
Three commands let you see what is set:
sf config list
sf config get target-org
sf config unset target-orgsf config list shows each key, its value, and whether it came from the local or global file. This is the first thing to check when a command targets the wrong org. sf config unset removes a value from the local scope by default, and from the global scope with --global. If you set the same key in both places, unsetting the local one reveals the global one again.
The Dev Hub default
A second key, target-dev-hub, holds the default Dev Hub org used by commands that create scratch orgs. A Dev Hub is an org that is allowed to create and manage scratch orgs. You can set it at login with --set-default-dev-hub (short form -d is for the default org, so check sf org login web --help for the exact short flags) or later with sf config set target-dev-hub. Commands that accept a Dev Hub take --target-dev-hub (-v) as the one-off override, in the same way --target-org overrides the default org.
How the CLI picks an org
When several sources name an org, the CLI uses a fixed order of precedence. The first match in this list wins:
- The
--target-orgflag on the command. - The
SF_TARGET_ORGenvironment variable. - The local
target-orgvalue in.sf/config.json. - The global
target-orgvalue in~/.sf/config.json.
Whichever source wins, the value then goes through alias resolution. If it matches an alias, the CLI swaps in the username. It then looks up the saved authorization for that username. The default is therefore stored as the text you typed, not as a username. If you later change what the alias myorg points to, the default follows it.
The environment variable is useful in scripts and continuous integration jobs, where you do not want to depend on a developer's config files:
export SF_TARGET_ORG=ci-sandbox
sf project deploy start --source-dir force-appCommon problems
No org specified. If no source in the list above supplies an org, org-based commands fail with a message saying a target org is required. Fix it by setting a default or passing --target-org.
The wrong org is targeted. This usually means a local config is overriding the global one, or SF_TARGET_ORG is set in your shell profile. Run sf config list and then echo $SF_TARGET_ORG to see both sources.
Authorization not found. If the value does not match any saved authorization, the CLI reports that it has no authorization information for that name. Causes include a typo in the alias, an org you never logged in to on this machine, or a scratch org that expired and was removed. Run sf org list to see what the CLI actually knows about, and sf alias list to see what each nickname points to.
Setting config outside a project. sf config set target-org myorg fails if you are not inside a DX project, because there is no local .sf folder to write to. Move into a project folder, or add --global.
Stale defaults after expiry. A scratch org default keeps pointing at the expired org. Commands then fail until you set a new default. Check sf org list for orgs marked as expired and clean them up.
An alias that shadows a username. Aliases are looked up before usernames. If you create an alias that is spelled exactly like a different username, commands may resolve to the alias target and not the org you meant. Use descriptive, non-email-shaped aliases such as prod, uat, or feature-123.
A recommended setup
For most developers, this arrangement keeps commands short and safe:
- Log in to each org with a clear alias, and do not set a default for production.
- Set a global default only for a sandbox or Developer Edition org you use every day.
- Inside each project, set a local
target-orgto that project's working org. - Pass
--target-org prodexplicitly for any command that touches production, so a default can never send a deploy there by accident.
Exercise
You run sf config set target-org dev1 --global. Inside a project folder, .sf/config.json contains "target-org": "uat". From that project folder you run sf data query --query "SELECT Id FROM Account LIMIT 1", with no org flag, and SF_TARGET_ORG is not set. Which org is queried? What changes if you add -o prod to the command?
Show answer
The query runs against uat. The local config value takes precedence over the global value dev1. With -o prod, the flag takes precedence over every config source, so the query runs against prod, and neither stored default changes.
Exercise
A teammate says sf org display shows the right org on their laptop, but the same script fails in a CI job with an error about a missing target org. The CI job has already authorized the org. What are two ways to fix the script, and which is more reliable for automation?
Show answer
Pass --target-org <alias> on each command, or set the SF_TARGET_ORG environment variable for the job. The CI machine has no .sf/config.json or global config with a default, which is why it fails. Both approaches avoid relying on config files. The explicit flag is more reliable, because it is visible in the script and cannot be overridden by leftover configuration.