Understanding Repository Context
Many commands in the Bitbucket CLI need to know which workspace and repository you’re working with. The CLI uses a sophisticated resolution system to determine this automatically, so you don’t always need to specify them explicitly.
Context Resolution Order
Section titled “Context Resolution Order”The CLI checks multiple sources in this order:
- Command-line flags (
--workspaceand--repo) - Current Git repository (automatically detected from your git remote URL)
- Config file defaults (
defaultWorkspacesetting) - Combinations of the above sources
Typical Local Layout
Section titled “Typical Local Layout”Directoryprojects/
Directorymyworkspace-tools/
Directory.git/
- …
Directorysrc/
Directorycommands/
- …
Directoryservices/
- …
- README.md
When you run bb from myworkspace-tools/, the CLI reads the Bitbucket remote from .git/ and uses that workspace/repo as context.
Examples
Section titled “Examples”Scenario 1: Inside a Git Repository
Section titled “Scenario 1: Inside a Git Repository”# You're in /projects/myrepo, which has a Bitbucket remotecd /projects/myrepobb pr list # ✅ Uses workspace/repo from git remoteThe CLI reads your .git/config and extracts the workspace and repository from the remote URL.
Scenario 2: Using Command-Line Flags
Section titled “Scenario 2: Using Command-Line Flags”# Explicit specification always takes precedencebb pr list -w myworkspace -r myrepo # ✅ Uses specified valuesThis works from anywhere, even outside a git repository.
Scenario 3: Using Config Defaults
Section titled “Scenario 3: Using Config Defaults”# Set defaults oncebb config set defaultWorkspace myworkspace
# Then use commands without flagsbb repo list # ✅ Uses defaultWorkspace from configFor PR commands, you still need repository context (from git remote or -r).
Scenario 4: Mixed Sources
Section titled “Scenario 4: Mixed Sources”# Inside a git repo with workspace "gitworkspace/gitrepo"cd /projects/myrepo
# Override just the workspacebb pr list -w anotherworkspace # Uses "anotherworkspace/gitrepo"
# Override just the repobb pr list -r anotherrepo # Uses "gitworkspace/anotherrepo"Repository Argument Formats
Section titled “Repository Argument Formats”Commands that accept a repository argument support multiple formats:
Full Format: workspace/repo
Section titled “Full Format: workspace/repo”bb repo view myworkspace/myrepobb repo delete myworkspace/myrepo --yesThis explicitly specifies both workspace and repository.
Short Format: repo only
Section titled “Short Format: repo only”# Uses default workspace from config or git contextbb repo view myrepoThe CLI will:
- Check if you set a default workspace via
bb config set defaultWorkspace - Check if you’re in a git repository and use its workspace
- Error if neither is available
Supported Remote URL Formats
Section titled “Supported Remote URL Formats”When detecting context from git, the CLI supports these remote URL formats:
SSH Format
Section titled “SSH Format”git@bitbucket.org:workspace/repo.gitgit@bitbucket.org:workspace/repoHTTPS Format
Section titled “HTTPS Format”https://bitbucket.org/workspace/repo.githttps://bitbucket.org/workspace/repohttps://username@bitbucket.org/workspace/repo.gitError Messages
Section titled “Error Messages””Could not determine repository”
Section titled “”Could not determine repository””This error appears when the CLI can’t figure out which workspace/repo to use.
Solutions:
- Use explicit flags:
-w myworkspace -r myrepo - Run from within a Bitbucket repository
- Set defaults:
bb config set defaultWorkspace myworkspace
”Repository not found”
Section titled “”Repository not found””The workspace/repo combination was determined but doesn’t exist.
Check:
- Spelling of workspace and repository names
- That you have access to the repository
- That the repository exists in that workspace
Best Practices
Section titled “Best Practices”For Daily Work
Section titled “For Daily Work”Set your default workspace if you primarily work with one:
bb config set defaultWorkspace myworkspaceThen work from within your git repositories - context will be automatic.
For Scripts
Section titled “For Scripts”Always use explicit flags for reliability:
#!/bin/bashWORKSPACE="myworkspace"REPO="myrepo"
bb pr list -w "$WORKSPACE" -r "$REPO" --jsonFor Multi-Workspace Teams
Section titled “For Multi-Workspace Teams”Don’t set a default workspace. Instead, use explicit flags or work from within cloned repositories:
# Each repository is self-containedcd project-a && bb pr list # Uses project-a's workspacecd ../project-b && bb pr list # Uses project-b's workspace- Use
bb repo viewwith no arguments to see what context the CLI detected - The
--jsonflag works with most commands for scripting - Global flags (
--workspace,--repo,--json,--no-color) are available across commands - The
-wshort flag always maps to--workspaceglobally