# 管理远程仓库

了解如何使用计算机上的本地仓库以及 GitHub 上托管的远程仓库。

## 添加远程仓库

要添加一个新的远程仓库，请在终端中进入存储库所在的目录，并使用 `git remote add` 命令。

`git remote add` 命令采用两个参数：

* 远程名称（例如 `origin`）
* 远程 URL（例如 `https://HOSTNAME/OWNER/REPOSITORY.git`）

例如：

```shell
$ git remote add origin https://HOSTNAME/OWNER/REPOSITORY.git
# Set a new remote

$ git remote -v
# Verify new remote
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)
```

有关要使用的 URL 的详细信息，请参阅 [关于远程仓库](/zh/enterprise-server@3.22/get-started/git-basics/about-remote-repositories)。

### 故障排除：远程原点已存在

此错误消息表示您尝试添加的远程与本地仓库中的远程名称相同。

```shell
$ git remote add origin https://HOSTNAME/octocat/Spoon-Knife.git
> fatal: remote origin already exists.
```

若要解决此问题，可以：

* 为新的远程设备使用不同的名称。
* 在添加新的远程之前，重命名现有的远程仓库。 有关详细信息，请参阅下面的[重命名远程仓库](#renaming-a-remote-repository)。
* 在添加新的远程之前，删除现有的远程仓库。 有关详细信息，请参阅下面的[删除远程仓库](#removing-a-remote-repository)。

## 更改远程仓库的 URL

`git remote set-url` 命令更改现有的远程存储库 URL。

> \[!TIP]
> 有关 HTTPS 和 SSH URL 之间区别的信息，请参阅 [关于远程仓库](/zh/enterprise-server@3.22/get-started/git-basics/about-remote-repositories)。

`git remote set-url` 命令采用两个参数：

* 现有远程仓库的名称。 例如，`origin` 或 `upstream` 是两个常见的选项。
* 远程仓库的新 URL。 例如：

  * 如果您要更新为使用 HTTPS，您的 URL 可能如下所示：

  ```shell
  https://HOSTNAME/OWNER/REPOSITORY.git
  ```

  * 如果您要更新为使用 SSH，您的 URL 可能如下所示：

  ```shell
  git@HOSTNAME:OWNER/REPOSITORY.git
  ```

### 将远程 URL 从 SSH 切换到 HTTPS

1. 打开<span class="platform-mac">终端</span><span class="platform-linux">终端</span><span class="platform-windows">Git Bash</span>。

2. 将当前工作目录更改为您的本地项目目录。

3. 列出现有远程仓库以获取要更改的远程仓库的名称。

   ```shell
   $ git remote -v
   > origin  git@HOSTNAME:OWNER/REPOSITORY.git (fetch)
   > origin  git@HOSTNAME:OWNER/REPOSITORY.git (push)
   ```

4. 使用 `git remote set-url` 命令将远程 URL 从 SSH 更改为 HTTPS。

   ```shell
   git remote set-url origin https://HOSTNAME/OWNER/REPOSITORY.git
   ```

5. 验证远程 URL 是否已更改。

   ```shell
   $ git remote -v
   # Verify new remote URL
   > origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
   > origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)
   ```

下次你执行 `git fetch`、`git pull` 或 `git push` 到远程存储库时，系统会要求你输入 GitHub 用户名和密码。 当 Git 提示输入密码时，请输入你的 personal access token密码。 或者，可以使用 [Git 凭据管理器](https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md)等凭据帮助程序。 Git 的基于密码的身份验证已被删除，取而代之的是更安全的身份验证方法。 有关详细信息，请参阅“[管理个人访问令牌](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)”。

你可以使用[凭据助手](/zh/enterprise-server@3.22/get-started/git-basics/caching-your-github-credentials-in-git)，这样每次 Git 与 GitHub 交互时，都会记住你的 GitHub 用户名和 personal access token 。

### 将远程 URL 从 HTTPS 切换到 SSH

1. 打开<span class="platform-mac">终端</span><span class="platform-linux">终端</span><span class="platform-windows">Git Bash</span>。

2. 将当前工作目录更改为您的本地项目目录。

3. 列出现有远程仓库以获取要更改的远程仓库的名称。

   ```shell
   $ git remote -v
   > origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
   > origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)
   ```

4. 使用 `git remote set-url` 命令将远程 URL 从 HTTPS 更改为 SSH。

   ```shell
   git remote set-url origin git@HOSTNAME:OWNER/REPOSITORY.git
   ```

5. 验证远程 URL 是否已更改。

   ```shell
   $ git remote -v
   # Verify new remote URL
   > origin  git@HOSTNAME:OWNER/REPOSITORY.git (fetch)
   > origin  git@HOSTNAME:OWNER/REPOSITORY.git (push)
   ```

### 故障排除：找不到远程 '\[name]'

此错误表示您尝试更改的远程不存在：

```shell
$ git remote set-url sofake https://HOSTNAME/octocat/Spoon-Knife
> fatal: No such remote 'sofake'
```

检查您是否正确键入了远程名称。

## 重命名远程仓库

使用 `git remote rename` 命令重命名现有远程。

`git remote rename` 命令采用两个参数：

* 现有远程名称（例如 `origin`）
* 远程的新名称（例如 `destination`）

### 重命名远程存储库的示例

这些示例假定[使用 HTTPS 进行克隆](/zh/enterprise-server@3.22/get-started/git-basics/about-remote-repositories#cloning-with-https-urls)（建议这样做）。

```shell
$ git remote -v
# View existing remotes
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)

$ git remote rename origin destination
# Change remote name from 'origin' to 'destination'

$ git remote -v
# Verify remote's new name
> destination  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
> destination  https://HOSTNAME/OWNER/REPOSITORY.git (push)
```

### 故障排除：无法将配置部分 'remote.\[old name]' 重命名为 'remote.\[new name]'

此错误表示您键入的旧远程名称不存在。

可以使用 `git remote -v` 命令检查当前存在的远程：

```shell
$ git remote -v
# View existing remotes
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)
```

### 故障排除：远程 \[new name] 已存在

此错误表示您要使用的远程名称已经存在。 要解决此问题，可以使用不同的远程仓库名称，或重命名原来的远程仓库。

## 删除远程仓库

使用 `git remote rm` 命令从存储库中删除远程 URL。

`git remote rm` 命令采用一个参数：

* 远程名称（例如 `destination`）

从存储库中删除远程 URL 只会取消本地和远程存储库的链接。 它不会删除远程存储库。

### 删除远程存储库的示例

这些示例假定[使用 HTTPS 进行克隆](/zh/enterprise-server@3.22/get-started/git-basics/about-remote-repositories#cloning-with-https-urls)（建议这样做）。

```shell
$ git remote -v
# View current remotes
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)
> destination  https://HOSTNAME/FORKER/REPOSITORY.git (fetch)
> destination  https://HOSTNAME/FORKER/REPOSITORY.git (push)

$ git remote rm destination
# Remove remote
$ git remote -v
# Verify it's gone
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (fetch)
> origin  https://HOSTNAME/OWNER/REPOSITORY.git (push)
```

> \[!NOTE]
> `git remote rm` 不会从服务器中删除远程存储库。 它只是从本地存储库中删除远程及其引用。

### 故障排除：无法删除配置节 'remote.\[name]'

此错误表示您尝试删除的远程不存在：

```shell
$ git remote rm sofake
> error: Could not remove config section 'remote.sofake'
```

检查您是否正确键入了远程名称。

## 延伸阅读

* ```
            [_《Pro Git》_ 一书中的“使用远程仓库”章节](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)
  ```