# GitLab Sync を有効にする

### 始める

GitLab リポジトリと同期したいスペースで、右上のスペースメニューを開き、次を選択します **Git と同期**を開き、 **GitLab Sync**、そして **設定**.

<figure><img src="/files/80603c3dc406f70f3af0ef4089c9a925bfa65b6f" alt="A GitBook screenshot showing GitLab Sync configuration options"><figcaption><p>GitLab 同期の設定オプション。</p></figcaption></figure>

### API アクセストークンを生成して入力する

API アクセストークンは、GitLab のユーザー設定で生成できます。

{% hint style="info" %}
GitLab には、Project と Personal の 2 種類のアクセストークンがあります。連携を動作させるには、Personal トークンを使用する必要があることに注意してください。これは GitLab のユーザー設定メニューから生成できます。
{% endhint %}

トークンに対して以下のアクセス権限を有効にしてください：

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

作成するトークンに特定のロールも付与されている場合は、そのロールが `Maintainer` または `Admin` ロールであることも確認してください。

その後、GitLab 連携を設定するときに、API アクセストークン欄へトークンを貼り付けます。

### リポジトリとブランチを選択する

GitBook のコンテンツと同期しておきたいリポジトリを選択します。

{% hint style="info" %}
**リポジトリが表示されませんか？** API トークンを作成するときに、正しい権限を設定したことを確認してください。
{% endhint %}

正しいリポジトリを選択したら、コミットをプッシュし同期する先のブランチを選択します。

{% hint style="warning" %}
多くの GitLab リポジトリでは、 `main` ブランチが自動的に保護対象に設定されている場合があります。その場合は、コンテンツを同期するための特定のブランチを追加することをおすすめします。その後、それを `main` にマージして、保護を維持できます。
{% endhint %}

### 初回同期を実行する

初めて同期する際は、次の 2 方向のいずれかで同期するオプションがあります。

1. GitBook -> GitLab では、スペースのコンテンツが同期されます **に** 選択したブランチへ同期します。空のリポジトリから始める場合や、GitBook のコンテンツをすぐに取り込みたい場合に最適です。
2. GitLab -> GitBook では、スペースのコンテンツが同期されます **から** 選択したブランチに対して。これは、リポジトリ内に既存の Markdown コンテンツがあり、それを GitBook に取り込みたい場合に最適です。

### 書き込みとコミット

これで準備完了です。スペースが [ライブ編集](/docs/documentation/ja-gitbook-documentation/collaboration/live-edits.md) モードでは、ライブ編集がロックされます。これにより、チームの誰かが[ 変更リクエスト](/docs/documentation/ja-gitbook-documentation/collaboration/change-requests.md) をマージしたときに、コンテンツをリポジトリへ確実に同期できます。

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/ja-gitbook-documentation/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.
