> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Git 集成

> 创建访问令牌，用 Git 克隆项目，然后执行 pull 和 push。

用 Git 在你自己的电脑上处理项目。只有在你运行 `git pull` 或 `git push` 时，更改才会传输。

> [!NOTE]
>
> 管理员需要先启用 [Git 集成](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/overleaf-toolkit/integrations/git-integration.md)。

## 创建令牌

Git 使用 Git 身份验证令牌登录，而不是你的账户密码。

**1. 打开账户设置**

点击 **Account**，然后点击 **Account settings**。在 **Git integration** 下，点击 **Generate token**。有了令牌之后，这个按钮会变为 **Add another token**。

![账户设置中的 Git integration 部分，带有 Generate token 按钮](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-settings.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=f4ff1f16eb46266f7bf31956bf3b9d89)

账户设置中的 Git 集成

**2. 复制令牌**

点击 **Copy**，并把令牌保存在安全的地方。它只会显示一次。然后点击 **Close**。

![带有 Copy 按钮的 Git 身份验证令牌对话框](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-token.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=8358a37df6799da34fd12c6503f619f5)

你的新令牌

**3. 检查令牌**

列表现在会显示令牌的开头、创建时间和上次使用时间。令牌一年后过期。你最多可以拥有 10 个令牌。

![包含一个令牌的令牌列表](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-tokens.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=e96fcc8b665f3005f950b7706fdf451c)

你的令牌

## 克隆项目

**1. 在编辑器中打开 Git 集成**

打开项目。在左侧栏中点击 **Integrations**，然后点击 **Git integration**。

![带有 Git integration 卡片的 Integrations 面板](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-panel.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=3fb82c63ba8d205191eadb5015bdefb5)

Integrations 面板

**2. 复制命令**

**Clone with Git** 对话框会显示命令，其中包含你的站点地址和项目 ID。点击 **Copy**。如果你还没有令牌，**Go to settings** 会在新标签页中打开你的账户设置。

![带有 git clone 命令的 Clone with Git 对话框](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-clone.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=f6d69046df7caf4cbee07f49e8dd6540)

克隆命令

**3. 运行命令**

把命令粘贴到终端中。当 Git 要求输入密码时，粘贴你的令牌。

```text theme={null}
$ git clone https://git@<your-site>/git/<project-id>
Cloning into '<project-id>'...
Password for 'https://git@<your-site>': <your-token>
```

现在，一个以项目 ID 命名的文件夹中包含项目的文件，位于 `main` 分支上。要自定义文件夹名称，请把名称添加到命令末尾。

Git 每次连接站点时都会询问令牌。要让它记住令牌，请启用 credential helper：Git 会再询问一次，之后就会保存令牌。`store` helper 会把令牌以未加密的形式保存在 `~/.git-credentials` 中。

```bash theme={null}
git config --global credential.helper store
```

## 拉取编辑器中的更改

在文件夹中运行：

```bash theme={null}
git pull
```

Git 会列出发生变化的文件。在编辑器中所做的更改会以名为 `Update on Overleaf.` 的提交出现。

```text theme={null}
Updating db007e3..c3e6332
Fast-forward
 main.tex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
```

## 推送你的更改

提交你的更改，然后推送：

```bash theme={null}
git add main.tex
git commit -m "Add a line from Git"
git push
```

输出的最后一行显示移动了的分支：

```text theme={null}
   53aca61..db007e3  main -> main
```

更改会出现在编辑器中，已打开的编辑器会自动更新。新文件、文件夹和重命名会显示在文件树中。在 **History** 中，你的更改被标记为 **You (via Git)**。那里不会显示你的提交信息。

![项目历史显示推送的行，标记为 You (via Git)](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-history.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=e2f4d5575612ebd75db3656cac1ea495)

项目历史中的一次推送

### 如果 Git 拒绝你的推送

如果项目在此期间发生了变化，Git 会拒绝推送：

```text theme={null}
 ! [rejected]        main -> main (fetch first)
```

先获取这些更改，然后再次推送：

```bash theme={null}
git pull --no-rebase --no-edit
git push
```

普通的 `git pull` 可能会以 `fatal: Need to specify how to reconcile divergent branches.` 停止。上面的 `--no-rebase` 选项就是为此准备的：它会合并两边的更改。

如果两边修改了相同的行，pull 会以 `CONFLICT (content): Merge conflict in main.tex` 停止。修复文件，然后运行：

```bash theme={null}
git add main.tex
git commit -m "Merge the changes from the editor"
git push
```

站点不接受以下两种情况：

* 其他分支：`! [remote rejected] feature -> feature (wrong branch)`。只有 `main` 分支，所以请把你的工作推送到那里。
* 强制推送：`! [remote rejected] main -> main (forced push prohibited)`。请改为把你的更改放在当前 `main` 之上。

### 如果 Git 提示 Authentication failed

```text theme={null}
remote: Enter your Git authentication token when prompted for a password.
fatal: Authentication failed for 'https://<your-site>/git/<project-id>/'
```

令牌错误或已被删除，或者你输入的是账户密码。如果你已经没有这个令牌，请创建一个新令牌，因为已有的令牌无法再次查看。

## 删除令牌

在 **Account settings** 的 **Git integration** 下，点击令牌旁边的删除图标，然后在对话框中点击 **Delete**。此后，Git 对该令牌会返回 `Authentication failed`。

![Delete Authentication token 对话框](https://mintcdn.com/ayakaleaf-pro/IsGnx0wnzqtV4hI8/images/on-premises/git-integration-delete-token.png?fit=max\&auto=format\&n=IsGnx0wnzqtV4hI8\&q=85\&s=1708062228778239c6465b0369614a18)

删除令牌

> [!NOTE]
>
> 关于只读协作者可以做什么，请参阅 [Git 集成](https://ayakaleaf-pro.mintlify.site/zh-CN/on-premises/configuration/overleaf-toolkit/integrations/git-integration.md) 的 Q\&A。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.