Quick Reference Card
A printable reference for Copybara’s most common commands, patterns, and syntax.
CLI Commands
Section titled “CLI Commands”# Basic migrationcopybara migrate copy.bara.sky
# Specify workflow namecopybara migrate copy.bara.sky workflow_name
# Dry run (preview without changes)copybara migrate copy.bara.sky --dry-run
# Force sync from specific commitcopybara migrate copy.bara.sky --last-rev <sha>
# Ignore no-op (don't fail if nothing to sync)copybara migrate copy.bara.sky --ignore-noop
# Validate config syntaxcopybara validate copy.bara.sky
# Show info about a workflowcopybara info copy.bara.skyWorkflow Modes
Section titled “Workflow Modes”| Mode | Commits | Use Case |
|---|---|---|
SQUASH | All → 1 | Clean history, detect duplicates |
ITERATIVE | 1 → 1 | Preserve history |
CHANGE_REQUEST | All → 1 PR | Import external contributions |
CHANGE_REQUEST_FROM_SOT | All → 1 PR | Export to PR-based destination |
Core Workflow Template
Section titled “Core Workflow Template”core.workflow( name = "default", origin = git.origin(url = "https://github.com/org/source"), destination = git.destination(url = "https://github.com/org/dest"), origin_files = glob(["**"], exclude = ["internal/**"]), destination_files = glob(["**"]), authoring = authoring.pass_thru("Bot <bot@example.com>"), mode = "SQUASH", transformations = [ # transformations here ],)Glob Syntax
Section titled “Glob Syntax”# Include everythingglob(["**"])
# Include specific pathsglob(["src/**", "docs/**"])
# Exclude patternsglob(["**"], exclude = ["*.secret", "internal/**"])
# Combine globs (union)glob(["src/**"]) + glob(["README.md"])
# Root-only matchingglob(["*.md"]) # Only root-level .md filesglob(["**/*.md"]) # All .md files recursivelyCommon Transformations
Section titled “Common Transformations”Move Files
Section titled “Move Files”# Rename filecore.move("old/path.txt", "new/path.txt")
# Move directorycore.move("src/", "lib/")
# Move everything into subdirectorycore.move("", "vendor/lib/")Replace Text
Section titled “Replace Text”# Simple replacementcore.replace( before = "internal.example.com", after = "public.example.com",)
# With regexcore.replace( before = "Copyright ${year}", after = "Copyright 2024", regex_groups = {"year": "[0-9]+"},)
# In specific files onlycore.replace( before = "PLACEHOLDER", after = "VALUE", paths = glob(["**/*.md"]),)Scrub Commit Messages
Section titled “Scrub Commit Messages”# Remove lines matching patternmetadata.scrubber("(?m)^INTERNAL:.*$")
# Remove and replacemetadata.scrubber( "SECRET-[0-9]+", replacement = "[REDACTED]",)
# Keep only first line (subject)metadata.scrubber("\n(.|\n)*", replacement = "")Modify Metadata
Section titled “Modify Metadata”# Add prefix to messagemetadata.add_header("Imported from internal repo")
# Expose labels in commit messagemetadata.expose_label("GITHUB_PR_NUMBER")
# Verify pattern exists (fail if not)metadata.verify_match("^[A-Z]+-[0-9]+", verify_no_match = False)Authoring Modes
Section titled “Authoring Modes”# Keep original author, use default for unknownsauthoring.pass_thru("Default <default@example.com>")
# Always use default authorauthoring.overwrite("Bot <bot@example.com>")
# Map authors with allowlistauthoring.allowed( default = "Bot <bot@example.com>", allowlist = ["user@company.com", "other@company.com"],)
# Map authors with whitelistauthoring.whitelisted( default = "Bot <bot@example.com>", whitelist = ["user@company.com"],)Origins & Destinations
Section titled “Origins & Destinations”# Basic gitgit.origin(url = "https://github.com/org/repo", ref = "main")git.destination(url = "https://github.com/org/repo", push = "main")
# With submodulesgit.origin(url = "...", submodules = "YES")
# First parent only (skip merge commits)git.origin(url = "...", first_parent = True)GitHub
Section titled “GitHub”# GitHub PR origin (for importing PRs)git.github_pr_origin( url = "https://github.com/org/repo", use_merge = True,)
# GitHub PR destination (create PRs)git.github_pr_destination( url = "https://github.com/org/repo", destination_ref = "main",)Gerrit
Section titled “Gerrit”git.gerrit_destination( url = "https://review.example.com/repo", fetch = "master", push_to_refs_for = "master",)Folder (Testing)
Section titled “Folder (Testing)”# Output to local folderfolder.destination()
# Read from local folderfolder.origin()State Tracking Labels
Section titled “State Tracking Labels”| Label | Purpose |
|---|---|
GitOrigin-RevId | Default state label (last synced commit) |
custom_rev_id = "X-Id" | Use custom label name |
GITHUB_PR_NUMBER | PR number when using github_pr_origin |
COPYBARA_INTEGRATE_REVIEW | For integrating external changes |
Environment Variables (Docker)
Section titled “Environment Variables (Docker)”COPYBARA_CONFIG=copy.bara.skyCOPYBARA_WORKFLOW=defaultCOPYBARA_SOURCEREF=mainCOPYBARA_OPTIONS="--ignore-noop"COPYBARA_SUBCOMMAND=migrateCommon Patterns
Section titled “Common Patterns”Private → Public Sync
Section titled “Private → Public Sync”core.workflow( origin = git.origin(url = "private-repo"), destination = git.destination(url = "public-repo"), origin_files = glob(["**"], exclude = [ "internal/**", "**/*.secret", ".github/workflows/**", ]), mode = "ITERATIVE", transformations = [ metadata.scrubber("INTERNAL:.*"), core.replace(before = "internal.co", after = "public.co"), ],)Import External PRs
Section titled “Import External PRs”core.workflow( name = "import-pr", origin = git.github_pr_origin(url = "public-repo"), destination = git.destination(url = "private-repo"), mode = "CHANGE_REQUEST", authoring = authoring.pass_thru("Bot <bot@co.com>"),)Monorepo Extraction
Section titled “Monorepo Extraction”core.workflow( origin_files = glob(["packages/mylib/**"]), transformations = [ core.move("packages/mylib/", ""), ],)Quick Debugging
Section titled “Quick Debugging”# Validate configcopybara validate copy.bara.sky
# Dry run to see what would happencopybara migrate copy.bara.sky --dry-run
# Use folder.destination() to inspect output locally# Change destination to folder.destination() temporarily
# Check state label in destinationgit log --oneline | grep GitOrigin-RevId
# Force re-sync from specific commitcopybara migrate copy.bara.sky --last-rev <sha> --force