Git Hooks
pgbranch can install a git hook that automatically switches database branches when you switch git branches.
How It Works
Section titled “How It Works”When you switch git branches with git checkout or git switch, the post-checkout hook runs pgbranch checkout <branch-name>.
git checkout feature-auth │ ▼ post-checkout hook │ ▼pgbranch checkout feature-auth │ ▼Database now matches git branchIf a matching pgbranch branch doesn’t exist, the hook silently continues without error.
Installing the Hook
Section titled “Installing the Hook”pgbranch hook installOutput:
Git hook installed successfully!Database branches will now switch automatically with git branches.This creates a post-checkout script in .git/hooks/.
Uninstalling the Hook
Section titled “Uninstalling the Hook”pgbranch hook uninstallOutput:
Git hook removed.How the Hook Works
Section titled “How the Hook Works”The installed hook script:
#!/bin/sh# pgbranch post-checkout hook
# Get the new branch nameBRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)
# Skip if not on a branch (e.g., detached HEAD)if [ -z "$BRANCH" ]; then exit 0fi
# Try to checkout the matching pgbranch branchpgbranch checkout "$BRANCH" 2>/dev/null || trueKey behaviors:
- Runs after every git checkout
- Silently fails if no matching branch exists
- Doesn’t block git operations on error
Branch Naming Strategy
Section titled “Branch Naming Strategy”For automatic switching to work well, use matching names:
# Git branchgit checkout -b feature-user-auth
# pgbranch branch (same name)pgbranch branch feature-user-authNaming Convention Tips
Section titled “Naming Convention Tips”| Git Branch | pgbranch Branch |
|---|---|
main | main |
develop | develop |
feature/user-auth | feature-user-auth |
feature/api-v2 | feature-api-v2 |
Workflow Example
Section titled “Workflow Example”Initial Setup
Section titled “Initial Setup”# Initialize pgbranchpgbranch init -d myapp_dev
# Create main branch from clean stategit checkout mainpgbranch branch main
# Install the hookpgbranch hook installDaily Workflow
Section titled “Daily Workflow”# Start feature workgit checkout -b feature-new-api# (hook runs, but no pgbranch branch exists yet)
# Create database branchpgbranch branch feature-new-api
# Work on feature, make changes...
# Switch to fix a bug on maingit checkout main# Hook automatically runs: pgbranch checkout main# Database is now in main state!
# Back to featuregit checkout feature-new-api# Hook automatically runs: pgbranch checkout feature-new-api# Database restored to feature state!Handling Missing Branches
Section titled “Handling Missing Branches”When the hook tries to checkout a non-existent branch:
git checkout feature-new# Hook: pgbranch checkout feature-new# pgbranch: branch 'feature-new' doesn't exist (silent)# Git checkout completes normallyYour git checkout succeeds, and the database stays on the previous branch.
Creating Missing Branches Automatically
Section titled “Creating Missing Branches Automatically”You can modify the hook to create branches automatically:
#!/bin/shBRANCH=$(git symbolic-ref --short HEAD 2>/dev/null)if [ -z "$BRANCH" ]; then exit 0fi
# Try checkout, or create if doesn't existif ! pgbranch checkout "$BRANCH" 2>/dev/null; then pgbranch branch "$BRANCH" 2>/dev/null || truefiExisting Hooks
Section titled “Existing Hooks”If you already have a post-checkout hook:
pgbranch hook installOutput:
Warning: post-checkout hook already existsDo you want to: 1. Append pgbranch to existing hook 2. Replace existing hook 3. Cancel
Choice [1/2/3]:Choose option 1 to keep both hooks working.
Hook Location
Section titled “Hook Location”The hook is installed at:
.git/hooks/post-checkoutYou can edit it manually if needed.
Disabling Temporarily
Section titled “Disabling Temporarily”To skip the hook for one checkout:
git checkout --no-verify mainOr temporarily uninstall:
pgbranch hook uninstallgit checkout mainpgbranch hook installPerformance
Section titled “Performance”The hook adds a small delay to git checkouts:
- Fast (no matching branch): ~50ms
- With branch switch: 1-5 seconds (depends on DB size)
For large databases, consider disabling the hook and switching manually when needed.
Troubleshooting
Section titled “Troubleshooting”Hook Not Running
Section titled “Hook Not Running”Check if the hook is executable:
ls -la .git/hooks/post-checkout# Should show: -rwxr-xr-xMake executable if needed:
chmod +x .git/hooks/post-checkoutWrong Branch After Checkout
Section titled “Wrong Branch After Checkout”If the database doesn’t match after git checkout:
-
Check if pgbranch branch exists:
Terminal window pgbranch branch -
Manually checkout:
Terminal window pgbranch checkout <branch-name>
Hook Errors
Section titled “Hook Errors”If the hook shows errors, check:
- pgbranch is in PATH
- pgbranch is initialized in the directory
- Database is accessible
Integration with CI/CD
Section titled “Integration with CI/CD”In CI/CD environments, you typically don’t want the hook:
# Clone without hooksgit clone --no-checkout repo.gitcd repogit checkout mainOr disable hooks:
git config core.hooksPath /dev/null