Skip to main content

Fork a repo

A fork is a new repository that shares code and visibility settings with the original “upstream” repository.

About forks

分支是一个新存储库,与原“上游”存储库共享代码和可见性设置。 分支通常用于在向上游存储库提出想法或修改之前进行迭代,比如在开放源代码项目中,或当用户没有对上游存储库的写入权限时。 For more information, see "Working with forks."

Propose changes to someone else's project

For example, you can use forks to propose changes related to fixing a bug. Rather than logging an issue for a bug you have found, you can:

  • Fork the repository.
  • Make the fix.
  • Submit a pull request to the project owner.

Use someone else's project as a starting point for your own idea.

Open source software is based on the idea that by sharing code, we can make better, more reliable software. For more information, see the "About the Open Source Initiative" on the Open Source Initiative.

For more information about applying open source principles to your organization's development work on your GitHub Enterprise Server instance, see GitHub's white paper "An introduction to innersource."

When creating your public repository from a fork of someone's project, make sure to include a license file that determines how you want your project to be shared with others. For more information, see "Choose an open source license" at choosealicense.com.

为深入了解开源,特别是如何创建和发展开源项目,我们制作了开源指南,它将通过推荐关于创建和维护开源项目存储库的最佳实践,帮助你培养一个健康发展的开源社区。 还可以免费学习 GitHub Skills 课程来维护开源社区。

Prerequisites

If you haven't yet, first set up Git and authentication with your GitHub Enterprise Server instance from Git. For more information, see "Set up Git."

Forking a repository

You might fork a project to propose changes to the upstream repository. In this case, it's good practice to regularly sync your fork with the upstream repository. To do this, you'll need to use Git on the command line. You can practice setting the upstream repository using the same octocat/Spoon-Knife repository you just forked.

  1. On your GitHub Enterprise Server instance, navigate to the octocat/Spoon-Knife repository.
  2. In the top-right corner of the page, click Fork. Fork button

若要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。

To create a fork of a repository, use the gh repo fork subcommand.

gh repo fork REPOSITORY

To create the fork in an organization, use the --org flag.

gh repo fork REPOSITORY --org "octo-org"

Cloning your forked repository

Right now, you have a fork of the Spoon-Knife repository, but you do not have the files in that repository locally on your computer.

  1. On your GitHub Enterprise Server instance, navigate to your fork of the Spoon-Knife repository.

  2. 在文件列表上方,单击 “代码”。 “代码”按钮

  3. 复制存储库的 URL。

    • 要使用 HTTPS 克隆存储库,在“HTTPS”下单击
    • 要使用 SSH 密钥克隆存储库,包括组织的 SSH 证书颁发机构颁发的证书,请单击“SSH”,然后单击
    • 要使用 GitHub CLI 克隆存储库,请单击“GitHub CLI”,然后单击 用于复制 URL 以使用 GitHub CLI 克隆存储库的剪贴板图标
  4. 打开终端终端Git Bash

  5. 将当前的工作目录更改为您想要存储克隆目录的位置。

  6. Type git clone, and then paste the URL you copied earlier. It will look like this, with your GitHub Enterprise Server username instead of YOUR-USERNAME:

    $ git clone https://hostname/YOUR-USERNAME/Spoon-Knife
  7. Press Enter. Your local clone will be created.

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

若要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。

To create a clone of your fork, use the --clone flag.

gh repo fork REPOSITORY --clone=true
  1. 在“文件”菜单中,单击“克隆存储库” 。

    Mac 应用程序中的克隆菜单选项

    Windows 应用程序中的克隆菜单选项

  2. 单击与要克隆的仓库位置对应的选项卡。 也可以单击 URL 手动输入存储库位置。

    克隆仓库菜单中的 Location(位置)选项卡

    克隆仓库菜单中的 Location(位置)选项卡

  3. 从列表中选择要克隆的仓库。

    克隆仓库列表

    克隆仓库列表

  4. 单击“选择...”并找到要克隆存储库的本地路径。

    选择按钮

    选择按钮

  5. 单击“克隆”。

    克隆按钮

    克隆按钮

Configuring Git to sync your fork with the upstream repository

When you fork a project in order to propose changes to the upstream repository, you can configure Git to pull changes from the upstream repository into the local clone of your fork.

  1. On your GitHub Enterprise Server instance, navigate to the octocat/Spoon-Knife repository.

  2. 在文件列表上方,单击 “代码”。 “代码”按钮

  3. 复制存储库的 URL。

    • 要使用 HTTPS 克隆存储库,在“HTTPS”下单击
    • 要使用 SSH 密钥克隆存储库,包括组织的 SSH 证书颁发机构颁发的证书,请单击“SSH”,然后单击
    • 要使用 GitHub CLI 克隆存储库,请单击“GitHub CLI”,然后单击 用于复制 URL 以使用 GitHub CLI 克隆存储库的剪贴板图标
  4. 打开终端终端Git Bash

  5. Change directories to the location of the fork you cloned.

    • To go to your home directory, type just cd with no other text.
    • To list the files and folders in your current directory, type ls.
    • To go into one of your listed directories, type cd your_listed_directory.
    • To go up one directory, type cd ...
  6. Type git remote -v and press Enter. You will see the current configured remote repository for your fork.

    $ git remote -v
    > origin  https://hostname/YOUR_USERNAME/YOUR_FORK.git (fetch)
    > origin  https://hostname/YOUR_USERNAME/YOUR_FORK.git (push)
  7. Type git remote add upstream, and then paste the URL you copied in Step 3 and press Enter. It will look like this:

    $ git remote add upstream https://hostname/ORIGINAL_OWNER/Spoon-Knife.git
  8. To verify the new upstream repository you have specified for your fork, type git remote -v again. You should see the URL for your fork as origin, and the URL for the upstream repository as upstream.

    $ git remote -v
    > origin    https://hostname/YOUR_USERNAME/YOUR_FORK.git (fetch)
    > origin    https://hostname/YOUR_USERNAME/YOUR_FORK.git (push)
    > upstream  https://hostname/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
    > upstream  https://hostname/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

Now, you can keep your fork synced with the upstream repository with a few Git commands. For more information, see "Syncing a fork."

若要详细了解 GitHub CLI,请参阅“关于 GitHub CLI”。

To configure a remote repository for the forked repository, use the --remote flag.

gh repo fork REPOSITORY --remote=true

To specify the remote repository's name, use the --remote-name flag.

gh repo fork REPOSITORY --remote-name "main-remote-repo"

Editing a fork

You can make any changes to a fork, including:

  • Creating branches: Branches allow you to build new features or test out ideas without putting your main project at risk.
  • Opening pull requests: If you want to contribute back to the upstream repository, you can send a request to the original author to pull your fork into their repository by submitting a pull request.

Find another repository to fork

Fork a repository to start contributing to a project. You can fork a private or internal repository to your personal account or an organization on your GitHub Enterprise Server instance where you have repository creation permissions, if settings for the repository and your enterprise policies allow forking.

Next Steps

You have now forked a repository, practiced cloning your fork, and configured an upstream repository.

  • For more information about cloning the fork and syncing the changes in a forked repository from your computer, see "Set up Git."

  • You can also create a new repository where you can put all your projects and share the code on GitHub. 为项目创建存储库后,可以将代码存储在 GitHub 中。 这提供了工作备份,你可以选择将其与其他开发人员共享。 有关详细信息,请参阅“创建存储库”。"

  • GitHub 中的每个仓库均归个人或组织所有。 您可以在 GitHub Enterprise Server 上连接和关注人员、仓库和组织以与之进行交互。 有关详细信息,请参阅“社交化”。

  • GitHub 有一个强大的支持社区,你可以在那里寻求帮助并与来自世界各地的人交谈。 加入 GitHub Community 上的对话。