跳至主要内容

从 GitHub Desktop 克隆和分叉代码库

您可以使用 GitHub Desktop 克隆和分叉 GitHub 上存在的代码库。

平台导航

关于本地代码库

GitHub 上的代码库是远程代码库。您可以使用 GitHub Desktop 克隆或分叉代码库,以在您的计算机上创建本地代码库。

您可以通过克隆代码库来创建您有权访问的任何 GitHub 代码库的本地副本。如果您拥有代码库或具有写入权限,则可以在本地和远程位置之间进行同步。有关更多信息,请参阅“在 GitHub Desktop 中同步您的分支”。

克隆代码库后,您推送到 GitHub 的任何更改都将影响原始代码库。要进行更改而不影响原始项目,您可以通过分叉代码库来创建单独的副本。您可以创建拉取请求,建议维护者将您分叉中的更改合并到原始上游代码库中。有关更多信息,请参阅“关于分叉”。

当您尝试使用 GitHub Desktop 克隆您没有写入权限的代码库时,GitHub Desktop 将提示您自动创建分叉。您可以选择使用您的分叉来贡献原始上游代码库,或者独立处理您自己的项目。任何现有分叉都默认会将更改贡献到其上游代码库。您可以随时修改此选择。有关更多信息,请参阅“管理分叉行为”。

您还可以直接从 GitHub 或 GitHub Enterprise 克隆代码库。有关更多信息,请参阅“从 GitHub 克隆代码库到 GitHub Desktop”。

克隆代码库

  1. 在**文件**菜单中,单击**克隆代码库**。

    Screenshot of the menu bar on a Mac. The "File" dropdown menu is expanded, and the "Clone Repository" option is highlighted with an orange outline.

    Screenshot of the "GitHub Desktop" menu bar on Windows. The "File" dropdown menu is expanded, and the "Clone Repository" option is highlighted with an orange outline.

  2. 单击与您要克隆的代码库位置对应的选项卡。您也可以单击**URL**手动输入代码库位置。

    Screenshot of the "Clone a repository" window. At the top of the window, tabs labeled "GitHub.com", "GitHub Enterprise" and "URL" are highlighted with an orange outline.

  3. 从代码库列表中,单击您要克隆的代码库。

    Screenshot of the "Clone a repository" window. The "github/docs" repository is highlighted with an orange outline.

  4. 要选择要克隆代码库的本地目录,请在“本地路径”字段旁边,单击**选择...**并导航到该目录。

    Screenshot of the "Clone a repository" window. A button, labeled "Choose", is highlighted with an orange outline.

  5. 在“克隆代码库”窗口底部,单击**克隆**。

分叉代码库

您可以在 GitHub 或 GitHub Desktop 上分叉代码库。有关在 GitHub 上分叉的信息,请参阅“分叉代码库”。

在 GitHub Desktop 中,如果您尝试克隆您没有写入权限的代码库,则会自动为您创建一个分叉。

  1. 在**文件**菜单中,单击**克隆代码库**。

    Screenshot of the menu bar on a Mac. The "File" dropdown menu is expanded, and the "Clone Repository" option is highlighted with an orange outline.

    Screenshot of the "GitHub Desktop" menu bar on Windows. The "File" dropdown menu is expanded, and the "Clone Repository" option is highlighted with an orange outline.

  2. 单击与您要克隆的代码库位置对应的选项卡。您也可以单击**URL**手动输入代码库位置。

    Screenshot of the "Clone a repository" window. At the top of the window, tabs labeled "GitHub.com", "GitHub Enterprise" and "URL" are highlighted with an orange outline.

  3. 从代码库列表中,单击您要克隆的代码库。

    Screenshot of the "Clone a repository" window. The "github/docs" repository is highlighted with an orange outline.

  4. 要选择要克隆代码库的本地目录,请在“本地路径”字段旁边,单击**选择...**并导航到该目录。

    Screenshot of the "Clone a repository" window. A button, labeled "Choose", is highlighted with an orange outline.

  5. 在“克隆代码库”窗口底部,单击**克隆**。

  6. 阅读“您计划如何使用此分叉?”窗口中的信息。

    • 如果您计划将此分叉用于贡献原始上游代码库,请单击**贡献到父项目**。
    • 如果您计划将此分叉用于与上游无关的项目,请单击**用于我自己的目的**。
  7. 单击**继续**。

管理分叉行为

您可以使用 GitHub Desktop 更改分叉在上游代码库中的行为方式。

  1. 在菜单栏中,选择**代码库**,然后单击**代码库设置...**。

    Screenshot of the menu bar on a Mac. In the expanded "Repository" dropdown menu, a cursor hovers over "Repository Settings", highlighted in blue.

    Screenshot of the "GitHub Desktop" menu bar on Windows. In the open "Repository" dropdown menu, an option labeled "Repository Settings" is outlined in orange.

  2. 在“代码库设置”窗口的左侧边栏中,单击**分叉行为**。

  3. 在“我将使用此分叉...”下,使用单选按钮选择您要如何使用分叉。

    Screenshot of the "Fork Behavior" pane. Two radio buttons, labeled "To contribute to the parent repository" and "For my own purposes", are outlined in orange.

  4. 单击**保存**。

为本地代码库创建别名

您可以为本地代码库创建别名,以帮助区分 GitHub Desktop 中同名的代码库。创建别名不会影响 GitHub 上代码库的名称。在代码库列表中,别名以斜体显示。

  1. 在 GitHub Desktop 左上角,当前仓库名称的右侧,点击.
  2. 右键点击您想创建别名的仓库,然后点击 **创建别名**。
  3. 输入仓库的别名。
  4. 点击 **创建别名**。

进一步阅读