跳至主要内容

克隆仓库

当你在 GitHub 上创建仓库时,它会作为远程仓库存在。你可以克隆你的仓库,在本地计算机上创建一个副本,并在两个位置之间同步。

平台导航

关于克隆仓库

你可以将仓库从 GitHub.com 克隆到本地计算机或代码空间,这样可以更方便地解决合并冲突、添加或删除文件以及推送较大的提交。克隆仓库时,你会把仓库从 GitHub.com 复制到本地机器,或者在创建代码空间时复制到远程虚拟机。有关克隆到代码空间的更多信息,请参阅为仓库创建 codespace

你可以将仓库从 GitHub.com 克隆到本地计算机,这样可以更方便地解决合并冲突、添加或删除文件以及推送较大的提交。克隆仓库时,你会把仓库从 GitHub.com 复制到本地机器。

你可以将仓库从 GitHub.com 克隆到本地计算机,这样可以更方便地解决合并冲突、添加或删除文件以及推送较大的提交。克隆仓库时,你会把仓库从 GitHub.com 复制到本地机器。

克隆仓库会下载 GitHub.com 当时拥有的完整仓库数据副本,包括项目中每个文件和文件夹的所有版本。你可以将更改推送到 GitHub.com 上的远程仓库,或从 GitHub.com 拉取他人的更改。有关更多信息,请参阅使用 Git

你可以克隆自己的已有仓库,或克隆他人已有的仓库以为项目做出贡献。

克隆仓库

  1. 在 GitHub 上,导航至仓库的主页面。

  2. 在文件列表上方,点击 代码

    Screenshot of the list of files on the landing page of a repository. The "Code" button is highlighted with a dark orange outline.

  3. 复制该仓库的 URL。

    • 要使用 HTTPS 克隆仓库,请在“HTTPS”下点击.

    • 要使用 SSH 密钥克隆仓库(包括由您组织的 SSH 证书颁发机构签发的证书),点击 SSH,然后点击.

    • 要使用 GitHub CLI 克隆仓库,点击 GitHub CLI,然后点击.

      Screenshot of the "Code" dropdown menu. To the right of the HTTPS URL for the repository, a copy icon is outlined in dark orange.

  4. 打开 终端终端Git Bash

  5. 将当前工作目录切换到希望放置克隆目录的位置。

  6. 键入 git clone,然后粘贴你之前复制的 URL。

    git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
    
  7. Enter 创建本地克隆。

    $ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
    > Cloning into `Spoon-Knife`...
    > remote: Counting objects: 10, done.
    > remote: Compressing objects: 100% (8/8), done.
    > remove: Total 10 (delta 1), reused 10 (delta 1)
    > Unpacking objects: 100% (10/10), done.
    

注意

要了解更多关于 GitHub CLI 的信息,请参阅 关于 GitHub CLI

要在本地克隆仓库,请使用 repo clone 子命令。将 repository 参数替换为仓库名称,例如 octo-org/octo-repomonalisa/octo-repoocto-repo。如果省略了 OWNER/ 部分(即 OWNER/REPO),则默认使用认证用户的名称。

gh repo clone REPOSITORY

你也可以使用 GitHub URL 克隆仓库。

gh repo clone https://github.com/PATH-TO/REPOSITORY
  1. 在 GitHub 上,导航至仓库的主页面。

  2. 在文件列表上方,点击 代码

    Screenshot of the list of files on the landing page of a repository. The "Code" button is highlighted with a dark orange outline.

  3. 要使用 GitHub Desktop 克隆并打开仓库,请点击 使用 GitHub Desktop 打开

    Screenshot of the "Code" dropdown for a repository. A button, labeled "Open with GitHub Desktop" is outlined in dark orange.

  4. 按照 GitHub Desktop 中的提示完成克隆。

更多信息,请参阅从 GitHub 克隆仓库到 GitHub Desktop

克隆空仓库

空仓库不包含任何文件。通常在创建仓库时如果没有使用 README 进行初始化,就会出现这种情况。

  1. 在 GitHub 上,导航至仓库的主页面。

  2. 要使用 HTTPS 通过命令行克隆你的仓库,在 “Quick setup” 下,点击. 要使用 SSH 密钥(包括组织的 SSH 证书颁发机构签发的证书)克隆仓库,点击 SSH,然后点击.

    Screenshot of the quick setup notes for an empty repository. To the right of the HTTPS URL for the repository, a copy icon is outlined in orange.

    另外,要在 Desktop 中克隆你的仓库,点击 在 Desktop 中设置 并按照提示完成克隆。

    Screenshot of the quick setup notes for an empty repository. The "Set up in Desktop" button is outlined in dark orange.

  3. 打开 终端终端Git Bash

  4. 将当前工作目录切换到希望放置克隆目录的位置。

  5. 键入 git clone,然后粘贴你之前复制的 URL。

    git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
    
  6. Enter 创建本地克隆。

    $ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
    > Cloning into `Spoon-Knife`...
    > remote: Counting objects: 10, done.
    > remote: Compressing objects: 100% (8/8), done.
    > remove: Total 10 (delta 1), reused 10 (delta 1)
    > Unpacking objects: 100% (10/10), done.
    

克隆错误排查

克隆仓库时可能会遇到一些错误。

如果无法克隆仓库,请检查以下事项:

延伸阅读

© . This site is unofficial and not affiliated with GitHub, Inc.