# 启用 GitLab 同步

### 入门指南

在你想要与 GitLab 仓库同步的空间中，前往右上角的空间菜单，并选择 **与 Git 同步**并选择 **GitLab Sync**，然后点击 **配置**.

<figure><img src="/files/456846ed90bdb881ad2896338f24f6490311a82f" alt="A GitBook screenshot showing GitLab Sync configuration options"><figcaption><p>GitLab 同步配置选项。</p></figcaption></figure>

### 生成并输入你的 API 访问令牌

你可以在 GitLab 用户设置中生成 API 访问令牌。

{% hint style="info" %}
GitLab 中有两种访问令牌：项目令牌和个人令牌。请注意，为了使集成正常工作，你需要使用个人令牌，你可以从 GitLab 用户偏好设置菜单中生成它。
{% endhint %}

请确保为你的令牌启用以下访问权限：

* `api`
* `read_repository`
* `write_repository`

如果你创建的令牌还附带了特定角色，也请确保它具有 `维护者` 或 `管理员` 角色。

然后，在配置 GitLab 集成时，你可以将该令牌粘贴到 API 访问令牌字段中。

### 选择一个仓库和分支

选择你想要与 GitBook 内容保持同步的仓库。

{% hint style="info" %}
**找不到你的仓库？** 创建 API 令牌时，请确保你已设置正确的权限。
{% endhint %}

选择正确的仓库后，选择你希望提交推送到并同步自哪个分支。

{% hint style="warning" %}
对于许多 GitLab 仓库， `main` 分支可能会自动设置为受保护分支。如果是这样，我们建议添加一个特定分支来同步你的内容。然后，你可以将其合并到 `main` 中，并保持保护设置不变。
{% endhint %}

### 执行初始同步

首次同步时，你可以选择两个方向之一进行同步：

1. GitBook -> GitLab 将同步你空间的内容 **所选分支。** 如果你是从一个空仓库开始，并希望快速将 GitBook 内容导入，这会非常适合。
2. GitLab -> GitBook 将同步你空间的内容 **自** 所选分支。这非常适合你在仓库中已有 markdown 内容，并希望将其导入 GitBook 的情况。

### 编写并提交

你已经可以开始了。你会注意到，如果你的空间处于 [实时编辑](/docs/documentation/zh/collaboration/live-edits.md) 模式下，实时编辑现在已被锁定。这使 GitBook 能够在你团队中的某人合并一个[ 更改请求](/docs/documentation/zh/collaboration/change-requests.md) 到 GitBook 时，我们就能可靠地将内容同步到你的仓库。

当你在 GitBook 中编辑时，每次变更请求合并都会在你选定的 GitLab 分支中生成一次提交。

当你提交到 GitLab 时，每次提交都会作为历史提交同步到你的 GitBook 空间。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.com/docs/documentation/zh/getting-started/git-sync/enabling-gitlab-sync.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
