> ## 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-TW/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-TW/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.