【Git】更改远程仓库的URL

git remote set-url 命令可更改现有远程仓库的 URL。

将远程 URL 从 SSH 切换到 HTTPS

  1. 打开 Git Bash。
  2. 将当前工作目录更改为您的本地仓库。
  3. 列出现有远程仓库以获取要更改的远程仓库的名称。
    $ git remote -v
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)
  4. 使用 git remote set-url 命令将远程的 URL 从 SSH 更改为 HTTPS。
    $ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
  5. 验证远程 URL 是否已更改。
    $ git remote -v
    # Verify new remote URL
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)

下次对远程仓库执行 git fetchgit pull 或 git push 操作时,您需要提供 GitHub 用户名和密码。 当 Git 提示您输入密码时,请输入您的个人访问令牌 (PAT)。 基于密码的身份验证对 Git 已弃用,使用 PAT 更安全。 更多信息请参阅“创建个人访问令牌”。

您可以使用凭据小助手让 Git 在每次与 GitHub 会话时记住您的 GitHub 用户名和个人访问令牌。

将远程 URL 从 HTTPS 切换到 SSH

  1. 打开 Git Bash。
  2. 将当前工作目录更改为您的本地仓库。
  3. 列出现有远程仓库以获取要更改的远程仓库的名称。
    $ git remote -v
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)
  4. 使用 git remote set-url 命令将远程的 URL 从 HTTPS 更改为 SSH。
    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
  5. 验证远程 URL 是否已更改。
    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)

posted on 2021-03-17 17:36  tuzhuo  阅读(743)  评论(0编辑  收藏  举报