Safe bidirectional sync between internal and public git repos.
You have an internal repo with proprietary code and you want to open-source parts of it. This creates two ongoing problems:
-
Leak risk. Internal files, code sections, and commit history must never reach the public repo. Standard git tools (fork, merge, cherry-pick) all carry or expose internal commits, and a single misstep exposes everything.
git filter-repocan strip history once, but rewrites SHAs on every run, breaking external clones and making it unusable for continuous sync. -
Silent divergence. Once two repos exist, they drift apart. Public contributions arrive on one side, internal development continues on the other, and without a disciplined process the repos become increasingly hard to reconcile: patches stop applying, filtered content falls out of sync, and nobody notices until it's a project to fix.
pubgate prepares branches for review. You create and merge PRs on your git host (GitHub, GitLab, etc.). It handles both directions:
- Stage changes behind an internal leak-review PR gate
- Push reviewed content to a PR branch on the public repo
- Absorb public contributions back into internal main with three-way merge
Filtering is mechanical: built-in ignore patterns exclude common internal/private/secret file naming conventions out of the box, BEGIN-INTERNAL / END-INTERNAL markers strip sections from individual files, and pubgate.toml is always excluded automatically. Custom ignore patterns in the config replace the defaults.
Core principle: the public repo is always an exact filtered copy of internal, never an independent fork. If external contributions arrive mid-cycle, publish bases the public PR on the last absorbed commit; git's three-way merge preserves them or surfaces conflicts. Divergence stays controlled and bounded.
The two workflows:
| Making changes public: absorb → stage → publish absorb is recommended first if the public repo has unabsorbed changes, but stage and publish proceed either way %%{init: {"flowchart": {"useMaxWidth": false, "nodeSpacing": 25, "rankSpacing": 30, "padding": 10}}}%%
flowchart TD
A["🔒 main (internal)"]
A -->|"stage"| B["🔒 pubgate/stage (internal)"]
B -->|"PR · leak review"| C["🔒 pubgate/public-approved (internal)"]
C ==>|"publish"| D["🌐 pubgate/publish (public)"]
D -->|"PR · publish check"| E["🌐 main (public)"]
style A fill:#2d5a2d,stroke:#4a4,color:#fff
style B fill:#1a3a1a,stroke:#4a4,color:#ccc
style C fill:#2d5a2d,stroke:#4a4,color:#fff
style D fill:#1a3a5a,stroke:#48f,color:#ccc
style E fill:#2d3a5a,stroke:#48f,color:#fff
|
Incorporating public contributions: absorb Run when the public repo has external contributions %%{init: {"flowchart": {"useMaxWidth": false, "nodeSpacing": 25, "rankSpacing": 30, "padding": 10}}}%%
flowchart TD
E2["🌐 main (public)"]
E2 -->|"absorb"| F["🔒 pubgate/absorb (internal)"]
F -->|"PR · merge review"| A2["🔒 main (internal)"]
style E2 fill:#2d3a5a,stroke:#48f,color:#fff
style F fill:#1a3a1a,stroke:#4a4,color:#ccc
style A2 fill:#2d5a2d,stroke:#4a4,color:#fff
|
- Python 3.10+ and
gitCLI - An existing internal repo with an
originremote - An existing public repo with at least one commit (e.g. a README created during repo setup)
- (Optional)
ghCLI authenticated viagh auth login. Enables automatic PR creation for GitHub-hosted repos. Without it, pubgate logs the manual steps instead. - (Optional)
azCLI with theazure-devopsextension, authenticated viaaz login. Enables automatic PR creation for Azure DevOps-hosted repos. The extension is installed automatically if missing. Without it, pubgate logs the manual steps instead. - (Optional) Git LFS if your repo uses LFS-tracked files. pubgate auto-detects LFS and handles pointer files automatically. Without it, LFS-specific operations are silently skipped.
- A clean worktree on
main, synced withorigin(no uncommitted changes, no unpushed commits)
- Install:
pip install pubgate
- Create
pubgate.tomlin repo root:Built-in ignore patterns cover common conventions (public_url = "https://github.com/you/public-repo.git"
.internal/*,internal/*,*-internal.*,*.internal.*,*_internal.*,*-private.*,*.private.*,*_private.*,*.secret,*.secrets). To override them, setignoreexplicitly (see Configuration). - Optionally, mark internal-only sections in files (in addition to ignore patterns, you can hide parts of individual files). Three comment styles are supported:
# BEGIN-INTERNAL secret_stuff() # END-INTERNAL
// BEGIN-INTERNAL secretStuff(); // END-INTERNAL
Markers must be properly paired. Nested, unclosed, or orphan<!-- BEGIN-INTERNAL --> <div class="secret">...</div> <!-- END-INTERNAL -->
END-INTERNALmarkers cause an error. After scrubbing, a residual check catches any surviving markers that were not removed. - Commit and push your changes to
main(direct push or via PR).pubgate absorbrequires a clean worktree synced withorigin. - Initialize tracking:
On first run, this records the current public repo HEAD as the starting point for future syncs. It creates a PR branch that records this baseline in a tracking file (
pubgate absorb
.pubgate-absorbed). Create a PR from that branch intomainon your git host and merge it. - After your first
pubgate stagerun creates thepubgate/public-approvedbranch, protect it on your git host: require pull requests (no direct pushes) and optionally require approvals. This ensures content only reaches the approved branch through reviewed PRs — the leak-review gate.
pubgate prepares branches for review. When a supported CLI is available (gh for GitHub, az for Azure DevOps) and the remote is a recognized host, pubgate automatically creates or updates the PR for each branch it pushes. Otherwise it logs the manual steps. Use --no-pr to skip automatic PR creation. After merging changes into internal main through your normal PR process:
- Recommended: run
pubgate absorbif the public repo has unabsorbed changes, then merge the absorb PR. This isn't required (stageandpublishproceed either way) but it keeps the public snapshot clean. - Run
pubgate stage. This creates apubgate/stagebranch with the filtered snapshot for leak review. - Review the PR from
pubgate/stage→pubgate/public-approved(created automatically on GitHub/Azure DevOps, or create it manually on other hosts). This is the leak-review gate. Review it to ensure no internal code is exposed. Merge when satisfied. - Run
pubgate publish. This delivers the reviewed content to the public repo as apubgate/publishbranch. - Review the PR from
pubgate/publish→mainon the public repo (created automatically on GitHub/Azure DevOps, or create it manually). Merge after CI passes.
Run when the public repo has external contributions that need to be brought into the internal repo.
- Run
pubgate absorb. This creates apubgate/absorbbranch with the merged public changes for review. - Review the PR from
pubgate/absorb→main(created automatically on GitHub/Azure DevOps, or create it manually on other hosts). - Resolve conflicts if any.
- Merge the PR.
Branches
Making changes public (absorb → stage → publish):
main(internal): internal development branch (protected)pubgate/stage(internal): branch for leak review: filtered internal content →pubgate/public-approvedpubgate/public-approved(internal): holds reviewed staged content approved for publication; created automatically on firststageif it doesn't exist. Protect this branch on your git host — require PRs (no direct pushes), and optionally require approvals. This is the leak-review gate: only content that passes PR review should land here.pubgate/publish(public): branch for publish review: reviewed content → publicmainmain(public): public-facing branch (protected)
Incorporating public contributions (absorb):
pubgate/absorb(internal): branch for merge review: public changes → internalmain
State files
.pubgate-absorbed(onmain): tracks which public commit was last absorbed.pubgate-staged(onpubgate/public-approved): tracks which internal commit was last staged
Created and updated automatically.
| Command | What it does |
|---|---|
pubgate stage |
Build a filtered snapshot of internal code and create a branch for leak review |
pubgate publish |
Push reviewed content to a PR branch on the public repo |
pubgate absorb |
Merge public contributions into an internal branch for review |
pubgate status |
Show sync status of absorb, stage, and publish (read-only, fetches remotes) |
Flags --dry-run, --force, and --no-pr apply to absorb, stage, and publish (not status). Flags come after the command; --repo-dir comes before it.
| Flag | Position | Description |
|---|---|---|
--dry-run |
after command | Show planned actions without writing branches or files. Still syncs with remotes to ensure accurate plans. Example: pubgate stage --dry-run |
--force |
after command | Overwrite an existing PR branch from a previous run whose PR was not yet merged. Without this flag, pubgate errors out if the PR branch already exists. Force-push is blocked on protected branches (main, pubgate/public-approved, and public main). Example: pubgate absorb --force |
--no-pr |
after command | Skip automatic PR creation even when a supported CLI (gh/az) is available. pubgate will still push the branch and log manual steps. Example: pubgate stage --no-pr |
--repo-dir |
before command | Run pubgate against a specific repo path instead of the current directory. Example: pubgate --repo-dir /path/to/repo stage |
Full pubgate.toml example (all fields shown with defaults, only public_url is required for first-time setup when the remote doesn't already exist):
# Internal repo
internal_main_branch = "main"
internal_approved_branch = "pubgate/public-approved"
internal_absorb_branch = "pubgate/absorb"
internal_stage_branch = "pubgate/stage"
# Public repo (public_url is required if the git remote isn't already configured)
public_url = "https://github.com/you/public-repo.git"
public_remote = "public-remote"
public_main_branch = "main"
public_publish_branch = "pubgate/publish"
# State tracking
absorb_state_file = ".pubgate-absorbed"
stage_state_file = ".pubgate-staged"
# Filtering (fnmatch syntax; patterns match against both full path and basename)
# These override the built-in defaults. Omit to use the defaults:
# .internal/* internal/* *-internal.* *.internal.* *_internal.*
# *-private.* *.private.* *_private.* *.secret *.secrets
ignore = [
".internal/*",
"*-internal.*",
"*.internal.*",
"*.secret",
]- Binary files: included as-is in staged snapshots (
BEGIN-INTERNALmarkers inside binaries are not processed); during absorb, binary modifications take the public version and are flagged for manual review. - Git LFS files: LFS pointers pass through all pipelines without modification. LFS files are treated as binary (never merged, never scrubbed for internal markers). pubgate runs
git lfs fetch/pushautomatically during absorb and publish. Use ignore patterns inpubgate.tomlto exclude sensitive LFS files from publication. If LFS is not installed, these operations are silently skipped. - Renames on public repo: the new path is copied in; the old file is kept locally and flagged for review.
- Deletions on public repo: deleted files are kept locally and flagged for review in the absorb PR.
- Merge conflicts: absorb uses three-way merge. Conflicts produce standard git conflict markers (
<<<<<<</=======/>>>>>>>) for manual resolution. - Sync artifacts: absorb excludes both state files (
.pubgate-absorbed,.pubgate-staged) from the diff (they are sync artifacts, not external contributions). When only state files changed since the last absorb, the resulting PR only updates.pubgate-absorbed(tracking-only). - Empty files after scrubbing: files that become empty after removing
BEGIN-INTERNALblocks are still included in the staged snapshot. - External contribution between stage and publish: if someone pushes to the public repo after you stage but before you publish,
publishstill proceeds: it bases the public PR on the last absorbed commit, and git's three-way merge preserves external contributions or surfaces conflicts in the public PR. For a clean snapshot, runabsorb→ merge absorb PR →stage→ merge stage PR →publish. - Stale branch cleanup: after you merge a PR and its source branch is auto-deleted on the server, pubgate automatically prunes the stale local branch on the next run. No manual cleanup needed.
- Commit messages: absorb commit messages list the public commits being absorbed (safe, they are already public). Stage commit messages list the internal commits since the last stage (safe, stays on the internal repo; useful context for the leak reviewer).
- Repeated publish without absorb: if you publish multiple times without running
absorbbetween cycles, each publish PR is based on the same absorbed commit. This produces a trivially resolvable merge conflict on.pubgate-stagedin the public PR (take the newer value). Runningabsorbbetween cycles avoids this. - Do not edit the pubgate PR branch directly: the
pubgate/publishbranch must only contain content produced bypublish. Manual edits to this branch before merging will be silently overwritten by the next publish cycle (they are not detected as external contributions). If published content needs a fix, make the change in the internal repo and re-runstage→publish.
| Error | Cause | Fix |
|---|---|---|
| "working tree is not clean" | Dirty worktree | Commit or stash your changes |
| "expected branch 'main', currently on '...'" | Not on the main branch | Run git checkout main |
| "HEAD is detached" | Detached HEAD state | Run git checkout main |
| "unpushed commit(s)" | Local main is ahead of origin |
Push your commits or reset |
| "behind" | Local main is behind origin |
Run git pull --rebase |
| "diverged" | Local main has diverged from origin |
Reconcile manually (rebase or reset) |
| "branch '...' already exists" | Previous PR not merged | Merge the PR, or use --force to overwrite |
| "no absorb state found" | First run, or absorb not yet done | Run pubgate absorb to create initial baseline |
| "no stage state found" | Stage PR not merged | Run pubgate stage and merge the internal PR |
| "has no 'main' branch" | Public repo is empty (no commits) | Push at least one commit to the public repo (e.g. add a README) |
# 1. Clone your internal repo and cd into it
git clone git@internal-host:you/internal-repo.git
cd internal-repo
# 2. Create pubgate.toml (built-in ignore patterns cover common conventions)
cat > pubgate.toml << 'EOF'
public_url = "https://github.com/you/public-repo.git"
EOF
git add pubgate.toml && git commit -m "Add pubgate config" && git push
# 3. Bootstrap - records the public repo's current HEAD as baseline
pubgate absorb
# Output: pushes pubgate/absorb branch
# → If gh/az CLI is set up, a PR is created automatically
# → Otherwise, go to your git host, create PR: pubgate/absorb → main
# → Merge the PR
# 4. Stage staged content (filters out internal files and scrubs markers)
pubgate stage
# Output: pushes pubgate/stage branch
# → PR: pubgate/stage → pubgate/public-approved (auto-created on GitHub/Azure DevOps)
# → Review for leaks, merge it
# 5. Publish to public repo
pubgate publish
# Output: pushes pubgate/publish to the public remote
# → PR: pubgate/publish → main on the public repo (auto-created on GitHub/Azure DevOps)
# → Merge after CI passes
# Done! For future syncs: absorb (if needed) → stage → publish.Requires Python 3.10+ and uv.
For design decisions and detailed command specifications, see SPEC.md.
uv sync # install dependencies
uv run pre-commit install # set up pre-commit hooks (ruff, ty, etc.)
uv run pytest # run tests (-n auto for parallel)
uv run pre-commit run -a # run all linting & formatting checksTests create temporary git repos locally. No network access needed.
MIT. See LICENSE.