---
title: "Automating Neon branch creation with Githooks"
description: "A Git post-checkout hook can create a Neon database branch for each new Git branch, giving every feature branch an isolated copy of the main Postgres database without a local install or seed scripts."
url: "https://swe.quest/articles/automating-neon-branch-creation-with-githooks-465e18efc9f92ca6"
canonical: "https://neon.com/blog/automating-neon-branch-creation-with-githooks"
source: "Neon"
published: "2023-05-25"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["version-control","postgres","developer-productivity"]
---

# Automating Neon branch creation with Githooks

A post on [Neon](https://swe.quest/sources/neon.md), published 2023-05-25, 8 min read, How-to. Original: [https://neon.com/blog/automating-neon-branch-creation-with-githooks](https://neon.com/blog/automating-neon-branch-creation-with-githooks)

## Summary

A Git post-checkout hook can create a Neon database branch for each new Git branch, giving every feature branch an isolated copy of the main Postgres database without a local install or seed scripts. The shell script reads the hook's arguments to detect branch checkouts and counts reflog entries to spot first-time checkouts. It asks the user for confirmation via /dev/tty, checks the Neon API for an existing branch, creates one with a POST request, and uses jq to write the returned connection URI to .env as DATABASE\_URL.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Version control](https://swe.quest/tags/version-control.md) (Engineering practices)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Developer productivity](https://swe.quest/tags/developer-productivity.md) (Engineering practices)

## Prerequisites

- shell scripting
- Git basics
- REST APIs ([easy posts on API design](https://swe.quest/tags/api-design.md?difficulty=easy))

## Keywords

Neon, Githooks, post-checkout, Neon API, jq, curl

## Related posts

- [A branch-first dev loop for Neon: link, checkout and env pull](https://swe.quest/articles/a-branch-first-dev-loop-for-neon-link-checkout-and-env-pull-3d4b71e97977636f.md): Neon · 2026-06-10 · Medium · 6 min read · Developer tools, PostgreSQL, AI for coding, Serverless
  Neon's CLI adds three commands for branch-first Postgres development. \`neon link\` binds a workspace to a project and branch via a git-ignored \`.neon\` file. \`neon checkout\` creates or switches database branches, much like \`git checkout -b\`. \`neon env pull\` writes only the Neon-managed connection strings and service secrets for the current branch into \`.env\`.
- [Branching Postgres databases with the Neon API](https://swe.quest/articles/branching-postgres-databases-with-the-neon-api-3faf0de941bc811a.md): Neon · 2022-12-07 · Medium · 3 min read · PostgreSQL, CI/CD, Testing
  Neon branches are copy-on-write clones of a Postgres database that can be created and deleted through the Neon API, giving CI pipelines an isolated copy of production data for end-to-end tests.
- [Just landed in the Neon CLI](https://swe.quest/articles/just-landed-in-the-neon-cli-10a12bf3c6b45d1f.md): Neon · 2026-08-25 · Medium · 6 min read · Developer tools, PostgreSQL, AI agents, Schema migrations
  The Neon CLI is renamed from neonctl to neon (old name kept as an alias) and gains a branch-first workflow built on \`neon link\`, \`neon checkout\` and \`neon env pull\`, which store project/branch context in a local .neon file and rewrite only Neon-managed env vars.
- [Adopting Neon branching in CI/CD pipelines: a practical story by Shepherd](https://swe.quest/articles/adopting-neon-branching-in-ci-cd-pipelines-a-practical-story-by-shepherd-7c4ee09d2fb5431a.md): Neon · 2024-07-11 · Medium · 5 min read · CI/CD, PostgreSQL, Schema migrations, Testing, Serverless
  Shepherd, an insurtech startup that moved its Alchemist pricing engine from SQLite to Postgres, uses Neon's copy-on-write database branches to give every pull request an ephemeral test database.
- [Fullstack preview environments using Neon and Qovery](https://swe.quest/articles/fullstack-preview-environments-using-neon-and-qovery-5ddb9f5e7c2f931c.md): Neon · 2023-08-11 · Medium · 8 min read · PostgreSQL, CI/CD, Deployment, Serverless
  Each Qovery preview environment can get its own isolated Postgres database by using a Qovery lifecycle job that creates a copy-on-write Neon branch on the environment's Start event and deletes it on Delete.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
