GitBook Git Sync: Branching Strategies That Keep Docs in Step with Code
Learn how to configure GitBook’s bidirectional Git Sync with a protected main branch, feature branches for drafts, and CI checks so documentation stays consistent without merge‑conflict fire drills.
06 Oct 2025, 13:44 UTC

The problem: docs drift when writers edit in GitBook and engineers edit in Git
Teams that adopt docs‑as‑code quickly discover a synchronization gap. A technical writer updates a page in the GitBook UI while a developer merges a markdown change in a pull request. Without a disciplined branching model the two edits collide, the sync stalls, and the published site shows stale or broken content.
Takeaway: Use a protected main branch as the single source of truth for published docs, keep all draft work on short‑lived feature branches, and enforce CI checks before any merge reaches main. This eliminates most merge‑conflict fire drills and gives you a reliable audit trail.
Connect the space to a repository
- In GitBook, open Settings → Git Sync and choose your Git provider (GitHub, GitLab, Bitbucket).
- Select a private repository (requires Business/Enterprise plan) and grant GitBook write access.
- Choose the branch that will represent the live site — typically
main— and enable Sync only from this branch.
After the first sync GitBook creates a commit that mirrors the current space content. From this point forward every UI edit pushes a commit to main, and every merged PR updates the published space automatically.
Branching strategy that works
1. Protected main for published docs
- Branch protection rules: require PR reviews, status checks, and linear history.
- Only the GitBook sync user (or a dedicated bot) pushes directly to
mainafter a successful CI run.
2. Feature branches for drafts
- Writers create a branch (e.g.,
docs/add‑api‑reference) frommain. - They edit locally or via the GitBook preview link that points to the branch (GitBook generates a preview URL for any branch that has been pushed).
- When ready, they open a PR targeting
main.
3. Optional long‑lived version branches
If you need to maintain an older major version (e.g., v1‑maintenance), create a separate GitBook space and link it to that branch. Avoid mapping many long‑lived branches to a single space — each extra version consumes a GitBook version slot and complicates sync.
Handling merge conflicts
Conflicts arise when the same file changes in GitBook UI and in a PR. GitBook surfaces a three‑way diff in its UI and pauses sync until the conflict is resolved.
- Preventive measure: Run a "docs‑only" bot user that performs all UI edits via the API. The bot commits to a short‑lived branch, opens a PR, and merges after CI — eliminating simultaneous edits on
main. - Manual resolution: If a conflict appears, open the GitBook conflict view, choose the correct version (or merge manually), then click Resolve & Sync. The sync resumes automatically.
CI/CD integration — validate before publish
Add a GitHub Actions (or GitLab CI) workflow that runs on every PR targeting main. The workflow lints markdown, checks links, and validates OpenAPI snippets using the GitBook CLI.
name: Docs CI
on:
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install GitBook CLI
run: npm ci # assumes @gitbook/cli in devDependencies
- name: Build and validate
run: npx @gitbook/cli build --gitbook-space=my-space --git-repo=owner/repo --branch=${{ github.head_ref }}
- name: Link check (optional)
run: npx markdown-link-check **/*.md
Where to run: On the CI runner (GitHub Actions, GitLab CI, etc.).
Permissions: Read access to the repository; write access only if you let the workflow push a resolved commit (rare).
Placeholders: Replace my-space, owner/repo with your actual space ID and repo.
Expected checks: Exit code 0 means markdown renders without errors; non‑zero fails the PR.
Risks: GitBook API rate limit (~100 req/min). Batch version creation or large migrations in a single job to avoid throttling.
Versioning pattern — tags map to GitBook versions
- Tag a release in Git:
git tag v2.0.0 && git push origin v2.0.0. - Use the GitBook Versions API (POST
/spaces/:spaceId/versions) to create a version namedv2.0pointing at that tag. - Readers can select the version from the GitBook UI;
maincontinues to track the latest draft.
Do not create a GitBook version for every feature branch — it inflates the version count and clutters the dropdown.
Assets and large binaries
Store images and PDFs under /assets in the repo. They sync natively. For files > 100 MB enable Git LFS:
git lfs track "assets/*.pdf"
git add .gitattributes assets/large.pdf
git commit -m "Add large PDF via LFS"
git push origin main
GitBook respects .gitattributes and downloads the LFS pointer during sync, keeping the repo lean.
Limitations you should know
- One space ↔ one repo. Monorepos with multiple doc spaces need separate repos or a custom CI router.
- Custom blocks (tabs, hints, API refs) are GitBook‑only. Raw markdown viewers show the raw syntax, which can confuse reviewers.
- Variables and snippets defined in the UI do not sync to Git. They remain in GitBook’s database, creating a split source of truth for reusable content.
- Private repo sync requires Business/Enterprise. Free/Pro plans only support public repos or GitBook‑hosted content.
- Webhook retries are not guaranteed. Implement idempotent CI triggers and a nightly reconciliation job for critical pipelines.
Quick verification checklist
- Run
npx @gitbook/cli buildlocally on the synced repo; compare the generated HTML with the GitBook preview. - Open a PR from a feature branch, let CI pass, merge, then confirm the published space reflects the change within a minute.
- Create a Git tag, call the Versions API, and verify the new version appears in the GitBook version dropdown.
- Push a 200 MB PDF via LFS; confirm GitBook serves the file without timeout.
If all four steps succeed, your branching model and sync pipeline are production‑ready.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.