Skip to main content

Contributing to projects

Learn how to contribute to a project through forking.

About forking

If you want to contribute to someone else's project but don't have write access to the repository, you can use a "fork and pull request" workflow.

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

You can contribute by submitting pull requests from your fork to the upstream repository. For more information, see "Fork a repo."

Forking a repository

This tutorial uses the Spoon-Knife project, a test repository that's hosted on GitHub.com that lets you test the fork and pull request workflow.

  1. Navigate to the Spoon-Knife project at https://github.com/octocat/Spoon-Knife.
  2. Click Fork. Fork button

Cloning a fork

You've successfully forked the Spoon-Knife repository, but so far, it only exists on GitHub Enterprise Server. To be able to work on the project, you will need to clone it to your computer.

You can clone your fork with the command line, GitHub CLI, or GitHub Desktop.

  1. On GitHub Enterprise Server, 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.
    > remove: 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. 单击“克隆”。

    克隆按钮

    克隆按钮

Creating a branch to work on

Before making changes to the project, you should create a new branch and check it out. By keeping changes in their own branch, you follow GitHub Flow and ensure that it will be easier to contribute to the same project again in the future. For more information, see "GitHub Flow."

git branch BRANCH-NAME
git checkout BRANCH-NAME
git branch BRANCH-NAME
git checkout BRANCH-NAME

For more information about how to create and manage branches in GitHub Desktop, see "Managing branches."

Making and pushing changes

Go ahead and make a few changes to the project using your favorite text editor, like Visual Studio Code. You could, for example, change the text in index.html to add your GitHub username.

When you're ready to submit your changes, stage and commit your changes. git add . tells Git that you want to include all of your changes in the next commit. git commit takes a snapshot of those changes.

git add .
git commit -m "a short description of the change"
git add .
git commit -m "a short description of the change"

For more information about how to stage and commit changes in GitHub Desktop, see "Committing and reviewing changes to your project."

When you stage and commit files, you essentially tell Git, "Okay, take a snapshot of my changes!" You can continue to make more changes, and take more commit snapshots.

Right now, your changes only exist locally. When you're ready to push your changes up to GitHub Enterprise Server, push your changes to the remote.

git push
git push

For more information about how to push changes in GitHub Desktop, see "Pushing changes to GitHub."

Making a pull request

At last, you're ready to propose changes into the main project! This is the final step in producing a fork of someone else's project, and arguably the most important. If you've made a change that you feel would benefit the community as a whole, you should definitely consider contributing back.

To do so, head on over to the repository on GitHub Enterprise Server where your project lives. For this example, it would be at https://github.com/<your_username>/Spoon-Knife. You'll see a banner indicating that your branch is one commit ahead of octocat:main. Click Contribute and then Open a pull request.

GitHub Enterprise Server will bring you to a page that shows the differences between your fork and the octocat/Spoon-Knife repository. Click Create pull request.

GitHub Enterprise Server will bring you to a page where you can enter a title and a description of your changes. It's important to provide as much useful information and a rationale for why you're making this pull request in the first place. The project owner needs to be able to determine whether your change is as useful to everyone as you think it is. Finally, click Create pull request.

Managing feedback

Pull Requests are an area for discussion. In this case, the Octocat is very busy, and probably won't merge your changes. For other projects, don't be offended if the project owner rejects your pull request, or asks for more information on why it's been made. It may even be that the project owner chooses not to merge your pull request, and that's totally okay. Your changes exist in your fork. And who knows--maybe someone you've never met will find your changes much more valuable than the original project.

Finding projects

You've successfully forked and contributed back to a repository. Go forth, and contribute some more!