{"meta":{"title":"管理远程仓库","intro":"了解如何使用计算机上的本地仓库以及 GitHub 上托管的远程仓库。","product":"开始","breadcrumbs":[{"href":"/zh/get-started","title":"开始"},{"href":"/zh/get-started/git-basics","title":"Git 基本知识"},{"href":"/zh/get-started/git-basics/managing-remote-repositories","title":"管理远程存储库"}],"documentType":"article"},"body":"# 管理远程仓库\n\n了解如何使用计算机上的本地仓库以及 GitHub 上托管的远程仓库。\n\n## 添加远程仓库\n\n要添加一个新的远程仓库，请在终端中进入存储库所在的目录，并使用 `git remote add` 命令。\n\n`git remote add` 命令采用两个参数：\n\n* 远程名称（例如 `origin`）\n* 远程 URL（例如 `https://github.com/OWNER/REPOSITORY.git`）\n\n例如：\n\n```shell\n$ git remote add origin https://github.com/OWNER/REPOSITORY.git\n# Set a new remote\n\n$ git remote -v\n# Verify new remote\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n有关要使用的 URL 的详细信息，请参阅 [关于远程仓库](/zh/get-started/git-basics/about-remote-repositories)。\n\n### 故障排除：远程原点已存在\n\n此错误消息表示您尝试添加的远程与本地仓库中的远程名称相同。\n\n```shell\n$ git remote add origin https://github.com/octocat/Spoon-Knife.git\n> fatal: remote origin already exists.\n```\n\n若要解决此问题，可以：\n\n* 为新的远程设备使用不同的名称。\n* 在添加新的远程之前，重命名现有的远程仓库。 有关详细信息，请参阅下面的[重命名远程仓库](#renaming-a-remote-repository)。\n* 在添加新的远程之前，删除现有的远程仓库。 有关详细信息，请参阅下面的[删除远程仓库](#removing-a-remote-repository)。\n\n## 更改远程仓库的 URL\n\n`git remote set-url` 命令更改现有的远程存储库 URL。\n\n> \\[!TIP]\n> 有关 HTTPS 和 SSH URL 之间区别的信息，请参阅 [关于远程仓库](/zh/get-started/git-basics/about-remote-repositories)。\n\n`git remote set-url` 命令采用两个参数：\n\n* 现有远程仓库的名称。 例如，`origin` 或 `upstream` 是两个常见的选项。\n* 远程仓库的新 URL。 例如：\n\n  * 如果您要更新为使用 HTTPS，您的 URL 可能如下所示：\n\n  ```shell\n  https://github.com/OWNER/REPOSITORY.git\n  ```\n\n  * 如果您要更新为使用 SSH，您的 URL 可能如下所示：\n\n  ```shell\n  git@github.com:OWNER/REPOSITORY.git\n  ```\n\n### 将远程 URL 从 SSH 切换到 HTTPS\n\n1. 打开<span class=\"platform-mac\">终端</span><span class=\"platform-linux\">终端</span><span class=\"platform-windows\">Git Bash</span>。\n\n2. 将当前工作目录更改为您的本地项目目录。\n\n3. 列出现有远程仓库以获取要更改的远程仓库的名称。\n\n   ```shell\n   $ git remote -v\n   > origin  git@github.com:OWNER/REPOSITORY.git (fetch)\n   > origin  git@github.com:OWNER/REPOSITORY.git (push)\n   ```\n\n4. 使用 `git remote set-url` 命令将远程 URL 从 SSH 更改为 HTTPS。\n\n   ```shell\n   git remote set-url origin https://github.com/OWNER/REPOSITORY.git\n   ```\n\n5. 验证远程 URL 是否已更改。\n\n   ```shell\n   $ git remote -v\n   # Verify new remote URL\n   > origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n   > origin  https://github.com/OWNER/REPOSITORY.git (push)\n   ```\n\n下次你执行 `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/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)”。\n\n你可以使用[凭据助手](/zh/get-started/git-basics/caching-your-github-credentials-in-git)，这样每次 Git 与 GitHub 交互时，都会记住你的 GitHub 用户名和 personal access token 。\n\n### 将远程 URL 从 HTTPS 切换到 SSH\n\n1. 打开<span class=\"platform-mac\">终端</span><span class=\"platform-linux\">终端</span><span class=\"platform-windows\">Git Bash</span>。\n\n2. 将当前工作目录更改为您的本地项目目录。\n\n3. 列出现有远程仓库以获取要更改的远程仓库的名称。\n\n   ```shell\n   $ git remote -v\n   > origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n   > origin  https://github.com/OWNER/REPOSITORY.git (push)\n   ```\n\n4. 使用 `git remote set-url` 命令将远程 URL 从 HTTPS 更改为 SSH。\n\n   ```shell\n   git remote set-url origin git@github.com:OWNER/REPOSITORY.git\n   ```\n\n5. 验证远程 URL 是否已更改。\n\n   ```shell\n   $ git remote -v\n   # Verify new remote URL\n   > origin  git@github.com:OWNER/REPOSITORY.git (fetch)\n   > origin  git@github.com:OWNER/REPOSITORY.git (push)\n   ```\n\n### 故障排除：找不到远程 '\\[name]'\n\n此错误表示您尝试更改的远程不存在：\n\n```shell\n$ git remote set-url sofake https://github.com/octocat/Spoon-Knife\n> fatal: No such remote 'sofake'\n```\n\n检查您是否正确键入了远程名称。\n\n## 重命名远程仓库\n\n使用 `git remote rename` 命令重命名现有远程。\n\n`git remote rename` 命令采用两个参数：\n\n* 现有远程名称（例如 `origin`）\n* 远程的新名称（例如 `destination`）\n\n### 重命名远程存储库的示例\n\n这些示例假定[使用 HTTPS 进行克隆](/zh/get-started/git-basics/about-remote-repositories#cloning-with-https-urls)（建议这样做）。\n\n```shell\n$ git remote -v\n# View existing remotes\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n\n$ git remote rename origin destination\n# Change remote name from 'origin' to 'destination'\n\n$ git remote -v\n# Verify remote's new name\n> destination  https://github.com/OWNER/REPOSITORY.git (fetch)\n> destination  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n### 故障排除：无法将配置部分 'remote.\\[old name]' 重命名为 'remote.\\[new name]'\n\n此错误表示您键入的旧远程名称不存在。\n\n可以使用 `git remote -v` 命令检查当前存在的远程：\n\n```shell\n$ git remote -v\n# View existing remotes\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n### 故障排除：远程 \\[new name] 已存在\n\n此错误表示您要使用的远程名称已经存在。 要解决此问题，可以使用不同的远程仓库名称，或重命名原来的远程仓库。\n\n## 删除远程仓库\n\n使用 `git remote rm` 命令从存储库中删除远程 URL。\n\n`git remote rm` 命令采用一个参数：\n\n* 远程名称（例如 `destination`）\n\n从存储库中删除远程 URL 只会取消本地和远程存储库的链接。 它不会删除远程存储库。\n\n### 删除远程存储库的示例\n\n这些示例假定[使用 HTTPS 进行克隆](/zh/get-started/git-basics/about-remote-repositories#cloning-with-https-urls)（建议这样做）。\n\n```shell\n$ git remote -v\n# View current remotes\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n> destination  https://github.com/FORKER/REPOSITORY.git (fetch)\n> destination  https://github.com/FORKER/REPOSITORY.git (push)\n\n$ git remote rm destination\n# Remove remote\n$ git remote -v\n# Verify it's gone\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n```\n\n> \\[!NOTE]\n> `git remote rm` 不会从服务器中删除远程存储库。 它只是从本地存储库中删除远程及其引用。\n\n### 故障排除：无法删除配置节 'remote.\\[name]'\n\n此错误表示您尝试删除的远程不存在：\n\n```shell\n$ git remote rm sofake\n> error: Could not remove config section 'remote.sofake'\n```\n\n检查您是否正确键入了远程名称。\n\n## 延伸阅读\n\n* ```\n            [_《Pro Git》_ 一书中的“使用远程仓库”章节](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)\n  ```"}