# Enable Git Sync

Connect a docs site to a GitHub repository or a GitLab project, each space in its own folder.

Git Sync is set up for a whole docs site: the site connects one repository and branch, and each of its
spaces syncs its own folder of it — for example `docs/guides` and `docs/api` — which suits monorepos.

You need the **creator** role on the site and on each of its spaces. On GitHub you need to be able to
install apps on the repository; on GitLab, the **Maintainer** role on the project.

{% tabs %}
{% tab title="GitHub" %}
{% stepper %}
{% step %}
### Connect GitHub

Open the site, then **Git Sync**, and choose **Connect GitHub**. Authorize the Docsheaf app. If it isn't
installed on your account or organization yet, GitHub asks you to install it: give it access to the
repositories you want to sync.
{% endstep %}

{% step %}
### Pick the repository, branch and folders

Back in Docsheaf, choose the repository and branch, and a **folder** like `docs` (leave it empty for the
repository root). Each space gets a folder inside it named after its URL slug (`docs/guides`,
`docs/api`); change any of them if you like.

Repository missing from the list? Choose **Add repositories** to give the app access on GitHub, then
**Refresh list**. **Install on another account** adds the app to another account or organization.
{% endstep %}

{% step %}
### Choose the first sync

- **Docsheaf → Git** — write each space to its folder. Use it for new, empty folders.
- **Git → Docsheaf** — read each folder into its space. Use it when the Markdown already exists.
{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="GitLab" %}
{% stepper %}
{% step %}
### Authorize Docsheaf

Open the site, then **Git Sync**, choose **Connect GitLab** and authorize Docsheaf with your GitLab
account. Docsheaf acts with your access from then on, for every space of the site.
{% endstep %}

{% step %}
### Pick the project, branch and folders

Choose a project where you're a Developer or above, the branch, and a **folder** like `docs`. Each
space gets a folder inside it named after its URL slug. Docsheaf adds a webhook to the project to hear
about pushes, which needs the Maintainer role.
{% endstep %}

{% step %}
### Choose the first sync

- **Docsheaf → Git** — write each space to its folder.
- **Git → Docsheaf** — read each folder into its space.
{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
When you import into a space that requires review, the first import opens a change request instead of
replacing the published content.
{% endhint %}

## Spaces you add later

A space you add to the site, move into it or restore from the trash gets its folder in the site's
folder, named after its URL slug, without connecting GitHub or GitLab again. If that folder already
holds Markdown, it's read into the space; if it's empty, the space is written to it.

A space moved to another site stops syncing with this one. A space in the trash keeps its folder but
doesn't sync until it's restored.

## Change a space's folder

In the site's **Git Sync**, choose **Change folder** next to the space, and whether its first sync
writes the space to the new folder or reads the folder into the space. The old folder stays in the
repository as it is.

Folders can't share files: two spaces can't use the same folder, a folder inside another space's, or the
repository root next to other folders. A space whose folder would overlap another's doesn't sync until
you **Choose a folder** for it.

## Sync now

Syncs run on their own after every push and every publish, and a sweep every 15 minutes catches anything
missed. Choose **Sync now** in the site's Git Sync settings to sync every space at once.

Each space's settings show the state of its folder: the last sync, any error, and a change request
waiting for review.

## Disconnect

Choose **Disconnect** in the site's Git Sync settings. Every space stops syncing; the spaces and the
repository are left exactly as they are.
