# Git

Manage the linkage between your GitBook space and external Git repositories, enabling commits, merges, and pull requests directly tied to your documentation.

## POST /spaces/{spaceId}/git/import

> Import a Git repository

```json
{"openapi":"3.1.0","info":{"title":"GitBook API","version":"0.0.1-beta"},"tags":[{"name":"space-git","description":"Manage the linkage between your GitBook space and external Git repositories, enabling commits, merges, and pull requests directly tied to your documentation.\n"}],"servers":[{"url":"{host}/v1","variables":{"host":{"default":"https://api.gitbook.com"}}}],"security":[{"user":[]}],"components":{"securitySchemes":{"user":{"type":"http","scheme":"bearer"}},"parameters":{"spaceId":{"name":"spaceId","in":"path","required":true,"description":"The unique id of the space","schema":{"$ref":"#/components/schemas/EntityId"}}},"schemas":{"EntityId":{"type":"string","pattern":"^[a-zA-Z0-9_-]+$","description":"A unique entity identifier"},"ImportGitRepository":{"type":"object","properties":{"url":{"type":"string","description":"URL of the Git repository to import. It can contain basic auth credentials."},"ref":{"type":"string","description":"Git ref to import in the format \"refs/heads/main\""},"repoCacheID":{"type":"string","description":"Unique identifier to use to cache the Git repository across multiple operations.","deprecated":true},"repoTreeURL":{"type":"string","description":"URL to use as a prefix for external file references."},"repoCommitURL":{"type":"string","description":"URL to use as a prefix for the commit URL."},"repoProjectDirectory":{"type":"string","description":"Path to a root directory for the project in the repository."},"timestamp":{"description":"The timestamp of the event that triggered this import. It ensures that Git sync import and export operations are executed in the same order on GitBook and on the remote repository.\n","$ref":"#/components/schemas/Timestamp"},"force":{"type":"boolean"},"standalone":{"type":"boolean","description":"If true, the import will generate a revision without updating the space primary content."},"gitInfo":{"description":"Optional metadata to store on the space about the Git provider","$ref":"#/components/schemas/UpdateSpaceGitInfo"}},"required":["url","ref"]},"Timestamp":{"type":"string","format":"date-time"},"UpdateSpaceGitInfo":{"type":"object","description":"Update metadata about the Git provider on the space","properties":{"provider":{"type":"string","description":"The git provider","enum":["github","gitlab"]},"url":{"type":"string","description":"The repository's tree URL"}}}}},"paths":{"/spaces/{spaceId}/git/import":{"post":{"operationId":"importGitRepository","summary":"Import a Git repository","tags":["space-git"],"parameters":[{"$ref":"#/components/parameters/spaceId"}],"responses":{"204":{"description":"Operation to import the repository has been started."}},"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/ImportGitRepository"}}}}}}}}
```

## POST /spaces/{spaceId}/git/export

> Export the to a Git repository

```json
{"openapi":"3.1.0","info":{"title":"GitBook API","version":"0.0.1-beta"},"tags":[{"name":"space-git","description":"Manage the linkage between your GitBook space and external Git repositories, enabling commits, merges, and pull requests directly tied to your documentation.\n"}],"servers":[{"url":"{host}/v1","variables":{"host":{"default":"https://api.gitbook.com"}}}],"security":[{"user":[]}],"components":{"securitySchemes":{"user":{"type":"http","scheme":"bearer"}},"parameters":{"spaceId":{"name":"spaceId","in":"path","required":true,"description":"The unique id of the space","schema":{"$ref":"#/components/schemas/EntityId"}}},"schemas":{"EntityId":{"type":"string","pattern":"^[a-zA-Z0-9_-]+$","description":"A unique entity identifier"},"ExportToGitRepository":{"type":"object","properties":{"url":{"type":"string","description":"URL of the Git repository to export to. It can contain basic auth credentials."},"ref":{"type":"string","description":"Git ref to push the commit to in the format \"refs/heads/main\""},"commitMessage":{"type":"string","description":"Message for the commit generated by the export"},"repoCacheID":{"type":"string","description":"Unique identifier to use to cache the Git repository across multiple operations.","deprecated":true},"repoTreeURL":{"type":"string","description":"URL to use as a prefix for external file references."},"repoCommitURL":{"type":"string","description":"URL to use as a prefix for the commit URL."},"repoProjectDirectory":{"type":"string","description":"Path to a root directory for the project in the repository."},"timestamp":{"description":"The timestamp of the event that triggered this export. It ensures that Git sync import and export operations are executed in the same order on GitBook and on the remote repository.\n","$ref":"#/components/schemas/Timestamp"},"force":{"type":"boolean"},"gitInfo":{"description":"Optional metadata to store on the space about the Git provider","$ref":"#/components/schemas/UpdateSpaceGitInfo"}},"required":["url","ref","commitMessage"]},"Timestamp":{"type":"string","format":"date-time"},"UpdateSpaceGitInfo":{"type":"object","description":"Update metadata about the Git provider on the space","properties":{"provider":{"type":"string","description":"The git provider","enum":["github","gitlab"]},"url":{"type":"string","description":"The repository's tree URL"}}}}},"paths":{"/spaces/{spaceId}/git/export":{"post":{"operationId":"exportToGitRepository","summary":"Export the to a Git repository","tags":["space-git"],"parameters":[{"$ref":"#/components/parameters/spaceId"}],"responses":{"204":{"description":"Operation to export the space has been started."}},"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/ExportToGitRepository"}}}}}}}}
```

## Get space Git info

> Get metadata about the Git Sync provider currently set up on the space.

```json
{"openapi":"3.1.0","info":{"title":"GitBook API","version":"0.0.1-beta"},"tags":[{"name":"space-git","description":"Manage the linkage between your GitBook space and external Git repositories, enabling commits, merges, and pull requests directly tied to your documentation.\n"}],"servers":[{"url":"{host}/v1","variables":{"host":{"default":"https://api.gitbook.com"}}}],"security":[{"user":[]}],"components":{"securitySchemes":{"user":{"type":"http","scheme":"bearer"}},"parameters":{"spaceId":{"name":"spaceId","in":"path","required":true,"description":"The unique id of the space","schema":{"$ref":"#/components/schemas/EntityId"}}},"schemas":{"EntityId":{"type":"string","pattern":"^[a-zA-Z0-9_-]+$","description":"A unique entity identifier"},"GitSyncState":{"type":"object","properties":{"repoName":{"type":"string","description":"Repository name."},"installationStatus":{"$ref":"#/components/schemas/GitSyncInstallationStatus"},"installationProvider":{"$ref":"#/components/schemas/GitSyncProvider"},"integration":{"type":"string","deprecated":true,"description":"The integration name providing the Git Sync."},"installationId":{"type":"string","description":"The ID of the Git Sync installation."},"url":{"type":"string","description":"The URL to the repository tree, used when rendering public content."},"updatedAt":{"description":"When the Git provider details were last updated","$ref":"#/components/schemas/Timestamp"}}},"GitSyncInstallationStatus":{"type":"string","enum":["unauthenticated","active","pending"]},"GitSyncProvider":{"type":"string","description":"The provider of the Git Sync installation.","enum":["github","gitlab","github-legacy"]},"Timestamp":{"type":"string","format":"date-time"}},"responses":{"NotFoundError":{"description":"Not Found","content":{"application/json":{"schema":{"type":"object","required":["error"],"properties":{"error":{"type":"object","properties":{"code":{"type":"integer","format":"int32","enum":[404]},"message":{"type":"string"}},"required":["code","message"]}}}}}}}},"paths":{"/spaces/{spaceId}/git/info":{"get":{"operationId":"getSpaceGitInfo","summary":"Get space Git info","description":"Get metadata about the Git Sync provider currently set up on the space.","tags":["space-git"],"parameters":[{"$ref":"#/components/parameters/spaceId"}],"responses":{"200":{"description":"The Git Sync info for the space","content":{"application/json":{"schema":{"$ref":"#/components/schemas/GitSyncState"}}}},"404":{"description":"No Git provider currently set up on the space","$ref":"#/components/responses/NotFoundError"}}}}}}
```

## DELETE /spaces/{spaceId}/git/legacy-installation

> Remove the legacy Git Sync installation from the space to be able to upgrade it to use the new Git integrations

```json
{"openapi":"3.1.0","info":{"title":"GitBook API","version":"0.0.1-beta"},"tags":[{"name":"space-git","description":"Manage the linkage between your GitBook space and external Git repositories, enabling commits, merges, and pull requests directly tied to your documentation.\n"}],"servers":[{"url":"{host}/v1","variables":{"host":{"default":"https://api.gitbook.com"}}}],"security":[{"user-internal":[]}],"components":{"securitySchemes":{"user-internal":{"type":"http","scheme":"bearer"}},"parameters":{"spaceId":{"name":"spaceId","in":"path","required":true,"description":"The unique id of the space","schema":{"$ref":"#/components/schemas/EntityId"}}},"schemas":{"EntityId":{"type":"string","pattern":"^[a-zA-Z0-9_-]+$","description":"A unique entity identifier"}}},"paths":{"/spaces/{spaceId}/git/legacy-installation":{"delete":{"operationId":"deleteLegacyGitInstallation","summary":"Remove the legacy Git Sync installation from the space to be able to upgrade it to use the new Git integrations","tags":["space-git"],"parameters":[{"$ref":"#/components/parameters/spaceId"}],"responses":{"204":{"description":"The legacy Git installation was already removed"},"205":{"description":"The legacy Git installation was successfully removed"}}}}}}
```


---

# 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/developers/gitbook-api/api-reference/spaces/git.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.
