Give every Git branch its own local PostgreSQL or MySQL database — automatically.
Your app keeps using the exact same database name and DATABASE_URL. Whenever you switch Git branches, pgtwig automatically swaps the database behind the scenes.
- Works on macOS, Linux, and Windows.
- One small binary with no external dependencies.
- Supports PostgreSQL (
--driver psql, default) and MySQL (--driver mysql), locally or in Docker. - Never blocks
git checkoutif your database or Docker is turned off.
Your active Git branch always uses <db_name>. Other branches are saved as <db_name>-<branch_hash>.
When you run git checkout <other-branch>:
pgtwigparks<db_name>under the old branch's hash to save the branch you are leaving.- If the branch you are switching to already has a saved database,
pgtwigrestores it back to<db_name>right away. - If it is a new branch,
pgtwigcopies a database for it:- If the new branch was created from the branch you just left, it copies from that branch.
- Otherwise, it copies from your default branch (like
main).
Grab the latest archive for your OS and CPU from the GitHub Releases page (darwin, linux, or windows for amd64 / arm64), unpack it, and place pgtwig (or pgtwig.exe) somewhere in your PATH (for example ~/.local/bin).
You need Go 1.22+ and either psql (for PostgreSQL) or mysql (for MySQL), installed locally or inside your Docker container.
go build -o ~/.local/bin/pgtwig ./cmd/pgtwigMake sure ~/.local/bin is in your PATH.
go build -o "$HOME\.local\bin\pgtwig.exe" .\cmd\pgtwigAdd %USERPROFILE%\.local\bin to your PATH so Git Bash can find pgtwig.
Run pgtwig init inside your Git project:
# Local PostgreSQL (default driver: psql, default user: postgres)
pgtwig init --db myapp_dev
# Docker PostgreSQL
pgtwig init --db myapp_dev --container my-postgres
# Local MySQL (default user: root)
pgtwig init --db myapp_dev --driver mysql
# Docker MySQL
pgtwig init --db myapp_dev --driver mysql --container my-mysqlBy default, pgtwig uses your current branch as the default branch (you can override it with --default-branch main).
Now just use Git as usual:
git checkout -b feature/billing # saves main's DB, makes a fresh copy for feature/billing
# run migrations or test changes...
git checkout main # saves feature/billing, brings back main's DB right away| Command | What it does |
|---|---|
pgtwig init --db <name> |
Sets up pgtwig in .git/config and adds the post-checkout hook. Optional flags: --driver psql|mysql, --default-branch, --container, --user. |
pgtwig list |
Shows your active database and every local Git branch with its database status. |
pgtwig reset [--force] |
Deletes the current branch's database and makes a fresh copy from the default branch. Does nothing if you are already on the default branch. |
pgtwig clean [--force] |
Finds and deletes saved databases for Git branches that no longer exist. |
Run pgtwig <command> --help to see all options for any command.
pgtwig never saves database passwords in .git/config.
- Local: Uses your normal
~/.pgpass,PGHOST,PGPORT, andPGUSERsettings. - Docker: Connects inside the container via
docker exec -iwithout-h, so standard Postgres images usually need no password. - Password env var: Set
PGTWIG_PGPASSWORDif a password is required.
export PGTWIG_PGPASSWORD="your-password"- Local: Uses your normal
~/.my.cnf/ MySQL client defaults (--userdefaults toroot). - Docker: Runs
docker exec -i <container> mysql ...inside your container. - Password env var: Set
PGTWIG_MYSQL_PASSWORD(forwarded asMYSQL_PWD).
export PGTWIG_MYSQL_PASSWORD="your-password"| Tool | Where it runs | How it works | Speed & disk use | Data diff / merge | Good for teams / CI? | Setup effort |
|---|---|---|---|---|---|---|
pgtwig |
Local machine (Postgres/MySQL, native or Docker) | Renames and copies databases on git checkout |
Fast when switching back; full copy on a brand-new branch | No | No (local only) | Very easy |
| Neon | Cloud (Postgres) | Instant cloud database branching | Instant, uses almost no extra disk | Schema diff | Yes (great for PR previews & CI) | Medium (cloud account) |
| Supabase Branching | Cloud (Postgres) | Spins up a preview Postgres per Git branch | Takes a few minutes per branch | No | Yes (GitHub PR previews) | Medium (Supabase + GitHub) |
| DBLab / ZFS / Btrfs | Local Linux or self-hosted server | Filesystem-level snapshots | Instant even for huge databases (100 GB+) | No | Optional | Hard (special filesystem setup) |
| Dolt / DoltgreSQL | Local or Cloud (MySQL / Postgres) | A separate database built like Git | Fast branches, shared storage | Yes (row-level diff, merge, push/pull) | Yes | Medium–Hard (replaces standard DB engine) |
| Migration + Seed scripts (Prisma, Flyway, Goose, etc.) | Anywhere | Wipe the DB and re-run migrations & seed data | Depends on how fast your seed script runs | Schema only | Yes (scripts live in Git) | Easy (you likely already have this) |
- You work locally with normal PostgreSQL, MySQL, or Docker and don't want to change your setup.
- Your local database is small or medium (a few MBs to a few GBs).
- You switch Git branches often and hate fixing broken migrations or losing your local test data.
- You want something free, offline, and automatic every time you run
git checkout.
- You want a fresh cloud Postgres database automatically created for every Pull Request or CI run.
- Your team needs to share branch databases online for reviews or QA.
- Your database is very large (tens of GBs to terabytes). Copying it normally would be too slow and fill up your disk.
- You want real Git features for your data itself—like seeing row-by-row diffs, merging data from two branches, or pushing data changes to teammates.
- Keep in mind it replaces standard MySQL/Postgres, so check if all your DB features/extensions work with it.
- Rebuilding and seeding your local database from scratch already takes only a second or two, and you don't care about keeping branch-specific test data around.
- New branches make a full copy: Switching back to a branch you already visited is fast (Postgres renames the DB; MySQL moves base tables), while creating a database for a brand-new branch copies the data on disk.
- MySQL table-level cloning: Because MySQL has no
CREATE DATABASE ... TEMPLATEorALTER DATABASE RENAME,pgtwigmoves/copies base tables (RENAME TABLE/CREATE TABLE ... LIKE+INSERT INTO ... SELECT). Views, triggers, stored routines, and foreign keys are not copied automatically on MySQL. - Open connections get closed on switch:
pgtwigdisconnects active sessions before renaming or copying a database. Your app or DB client will simply reconnect. - Local only: Saved branch databases stay on your computer and are not pushed to Git.
- Database name length: Your base database name can be at most 54 bytes for PostgreSQL (63-byte limit) or 55 bytes for MySQL (64-byte limit).
go vet ./...
go test -v ./...
go build -o ~/.local/bin/pgtwig ./cmd/pgtwigTo publish a release with prebuilt macOS, Linux, and Windows binaries, push a vX.Y.Z tag:
git tag v0.1.0
git push origin v0.1.0GitHub Actions (release.yml) will run the tests, build binaries for macOS, Linux, and Windows (amd64 and arm64), generate checksums.txt, and create a GitHub Release automatically.