Keep your database in sync with your git branches.
Project description
git-db
Keep your database in sync with your git branches.
git-db is a developer tool for projects where database state follows code
changes: schema migrations, seed data, experimental feature work, and branch
switching during reviews. It installs a git post-checkout hook and keeps your
local database aligned with the branch you are working on.
Status: PostgreSQL is supported today; support for additional database engines is planned.
Features
- Automatic database handling on
git checkout - Two workflows:
shared: one database, saved and restored per branchper-branch: one database per branch
- PostgreSQL support today, with plans for more database backends
- Two PostgreSQL snapshot strategies:
template: fast database clones usingCREATE DATABASE ... TEMPLATEpgdump: portable snapshots usingpg_dumpandpg_restore
- Manual
save,restore,create,reset,list,status, andprunecommands - Safe hook behavior: checkout is never blocked by git-db failures
- Rich terminal output and local state stored under
.git/git-db/
Quick Start
uv tool install git-db # or pip install git-db
Run this from inside a git repository:
git-db init --database-url postgresql://postgres:postgres@localhost:5432/myapp
Interactive init will ask:
- Whether to use
sharedorper-branchmode - Whether to use
templateorpgdumpstrategy - What to do when active connections block database operations
- Whether to install the git
post-checkouthook
After setup, switch branches normally:
git checkout feature/auth
In shared mode, git-db saves the previous branch database and restores the
new branch snapshot if one exists.
In per-branch mode, git-db creates or selects a database named from the
current branch, for example:
myapp__feature__auth
Because the database name changes per branch, your application server also
needs to connect to the branch database. For example, when working on
feature/auth, point your app's DATABASE_URL at myapp__feature__auth.
Choosing a Mode
Shared Mode
Shared mode keeps one database name from DATABASE_URL.
Use this when:
- You want one familiar local database name
- You want branch-specific snapshots
- You are comfortable with git-db dropping and restoring that local database during branch switches
Per-Branch Mode
Per-branch mode creates a separate database for each branch. The configured default branch keeps the original database name and acts as the seed database.
Use this when:
- You want branch databases to persist independently
- You prefer creating new databases over repeatedly restoring one shared database
Choosing a Strategy
template
The template strategy uses PostgreSQL database cloning:
CREATE DATABASE target TEMPLATE source;
It is usually fast, but requires sufficient PostgreSQL privileges and can be blocked by active connections to the source or target database.
pgdump
The pgdump strategy uses pg_dump and pg_restore.
It is slower than template, but can be a better fit when template cloning is
not available. It requires PostgreSQL client tools to be installed locally.
Commands
Initialize
git-db init --database-url postgresql://user:password@localhost:5432/myapp
Useful options:
git-db init \
--database-url postgresql://user:password@localhost:5432/myapp \
--mode per-branch \
--strategy template \
--on-active-connections terminate
Skip hook installation:
git-db init --database-url postgresql://localhost/myapp --no-hook
Inspect State
git-db status
git-db list
Shared Mode Commands
Save the current branch database:
git-db save
Restore the current branch database:
git-db restore
Save or restore a specific branch:
git-db save main
git-db restore feature/auth
Per-Branch Commands
Create a branch database before checking out the branch:
git-db create feature/auth
Drop and recreate a branch database from the seed database:
git-db reset feature/auth
The default branch database cannot be reset because it is the seed for other branch databases.
Prune Deleted Branches
Preview stale snapshots or branch databases:
git-db prune --dry-run
Remove stale snapshots or branch databases:
git-db prune --yes
Hook Management
Install or reinstall the checkout hook:
git-db hook install
Remove the checkout hook:
git-db hook remove
Temporarily disable git-db without removing the hook:
git-db disable
git-db enable
You can also skip hook behavior for a single checkout:
GIT_DB_SKIP=1 git checkout other-branch
Configuration
git-db init writes .git-db.toml at the repository root.
Example:
database_url = "postgresql://postgres:postgres@localhost:5432/myapp"
mode = "per-branch"
default_branch = "main"
strategy = "template"
on_active_connections = "terminate"
Supported configuration keys:
| Key | Description | Default |
|---|---|---|
database_url |
Database connection URL | required |
mode |
shared or per-branch |
shared |
default_branch |
Seed branch for per-branch mode | main |
strategy |
template or pgdump |
required |
on_active_connections |
terminate or fail |
terminate |
snapshot_dir |
Shared-mode snapshot metadata/dump directory | .git/git-db/snapshots |
max_snapshots |
Snapshot count kept by prune logic | 20 |
force_terminate_timeout_ms |
Active connection termination timeout | 5000 |
Configuration precedence:
- Built-in defaults
.git-db.toml- Environment variables
- CLI options
Environment variables:
DATABASE_URL
GIT_DB_DATABASE_URL
GIT_DB_MODE
GIT_DB_STRATEGY
GIT_DB_ON_ACTIVE_CONNECTIONS
GIT_DB_SNAPSHOT_DIR
GIT_DB_MAX_SNAPSHOTS
GIT_DB_FORCE_TERMINATE_TIMEOUT_MS
GIT_DB_DATABASE_URL takes precedence over DATABASE_URL.
Active connections block an operation
Stop your development server, database console, migration watcher, or GUI client, then retry.
Alternatively, configure:
on_active_connections = "terminate"
Your PostgreSQL user may need superuser privileges or membership in
pg_signal_backend to terminate sessions owned by other users.
Temporarily skip git-db
git-db disable
git checkout some-branch
git-db enable
Or for one command:
GIT_DB_SKIP=1 git checkout some-branch
Show full tracebacks
GIT_DB_DEBUG=1 git-db status
Development
Install dependencies:
uv sync --group dev
Run checks:
uv run ruff check .
uv run mypy src tests
uv run pytest tests/unit
Run the full nox suite:
nox
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file db_git-0.1.0.tar.gz.
File metadata
- Download URL: db_git-0.1.0.tar.gz
- Upload date:
- Size: 90.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18ce1afceb65ca06d573174c343ad9c5f0e20346f5ab05ae33b8b56d014d2a80
|
|
| MD5 |
05e3d2a303baa6b12186afcca6ef4563
|
|
| BLAKE2b-256 |
ab188b26c05d849188fe38e17fecbf698f08749df8aa0499ecd61be373275bae
|
File details
Details for the file db_git-0.1.0-py3-none-any.whl.
File metadata
- Download URL: db_git-0.1.0-py3-none-any.whl
- Upload date:
- Size: 36.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9414fa2a0e17a9b247ec057a7492b4dacc88036687796514384c964746adc5d9
|
|
| MD5 |
4cf2bde5eab59ec8e2704b48143d416a
|
|
| BLAKE2b-256 |
2fd2477465d5adf9c6086083720f2f8b31a402f635b9e514c186aca3298fcff2
|