| title | Fork a repository | |||||||
|---|---|---|---|---|---|---|---|---|
| allowTitleToDifferFromFilename | true | |||||||
| redirect_from |
|
|||||||
| intro | Fork a repository on {% data variables.product.github %} to propose changes, collaborate on projects, and manage your own copy of the codebase. | |||||||
| permissions | {% data reusables.enterprise-accounts.emu-permission-fork %} | |||||||
| versions |
|
|||||||
| shortTitle | Fork a repository | |||||||
| category |
|
|||||||
| contentType | how-tos |
Forking a repository lets you propose changes to a project without affecting the upstream repository. See AUTOTITLE.
If you haven't already, set up Git and authentication with {% data variables.location.product_location %} from Git. See AUTOTITLE.
{% webui %}
You might fork a project to propose changes to the upstream repository. In this case, it's good practice to sync your fork with the upstream repository regularly. 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.
-
On {% ifversion fpt or ghec %}{% data variables.product.prodname_dotcom %}{% else %}{% data variables.location.product_location %}{% endif %}, navigate to the octocat/Spoon-Knife repository.
-
In the top-right corner of the page, click Fork.
-
Under "Owner," select the dropdown menu and click an owner for the forked repository.
-
By default, forks are named the same as their upstream repositories. Optionally, in the "Repository name" field, type a different name to distinguish your fork.
-
Optionally, in the "Description" field, type a description of your fork.
-
Optionally, select Copy the DEFAULT branch only.
For many forking scenarios, such as contributing to open-source projects, you only need to copy the default branch. If you do not select this option, all branches will be copied into the new fork.
-
Click Create fork.
Note
If you want to copy additional branches from the upstream repository, you can do so from the Branches page. See AUTOTITLE.
{% endwebui %}
{% cli %}
{% data reusables.cli.cli-learn-more %}
To create a fork of a repository, use the gh repo fork subcommand.
gh repo fork REPOSITORYTo create the fork in an organization, use the --org flag.
gh repo fork REPOSITORY --org "octo-org"{% endcli %}
{% desktop %}
You can fork a repository on {% data variables.product.prodname_dotcom_the_website %} or in {% data variables.product.prodname_desktop %}. For information about forking on {% data variables.product.prodname_dotcom_the_website %}, see the web browser version of this article.
{% data reusables.desktop.forking-a-repo %}
{% enddesktop %}
{% webui %}
You now have a fork of the Spoon-Knife repository, but you do not have the files from that repository on your computer.
-
On {% ifversion fpt or ghec %}{% data variables.product.prodname_dotcom %}{% else %}{% data variables.location.product_location %}{% endif %}, navigate to your fork of the Spoon-Knife repository. {% data reusables.repositories.copy-clone-url %}
-
Open your terminal or Git Bash. {% data reusables.command_line.change-current-directory-clone %}
-
Type
git clone, then paste the URL you copied earlier. It will look like this, with your {% data variables.product.github %} username instead ofYOUR-USERNAME:git clone https://{% data variables.product.product_url %}/YOUR-USERNAME/Spoon-Knife -
Press Enter. Git creates your local clone.
$ git clone https://{% data variables.product.product_url %}/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.
{% endwebui %}
{% cli %}
You now have a fork of the Spoon-Knife repository, but you do not have the files from that repository on your computer.
{% data reusables.cli.cli-learn-more %}
To create a clone of your fork, use the --clone flag.
gh repo fork REPOSITORY --clone=true{% endcli %}
When you fork a project 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.
{% webui %}
-
On {% ifversion fpt or ghec %}{% data variables.product.prodname_dotcom %}{% else %}{% data variables.location.product_location %}{% endif %}, navigate to the octocat/Spoon-Knife repository. {% data reusables.repositories.copy-clone-url %}
-
Open your terminal or Git Bash.
-
Change to the directory for the fork you cloned.
- To go to your home directory, type just
cdwith 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 ...
- To go to your home directory, type just
-
Type
git remote -vand press Enter. You will see the remote repository currently configured for your fork.$ git remote -v > origin https://{% data variables.product.product_url %}/YOUR-USERNAME/YOUR-FORK.git (fetch) > origin https://{% data variables.product.product_url %}/YOUR-USERNAME/YOUR-FORK.git (push)
-
Type
git remote add upstream, then paste the URL you copied in Step 3 and press Enter. It will look like this:git remote add upstream https://{% data variables.product.product_url %}/ORIGINAL-OWNER/Spoon-Knife.git -
To verify the new upstream repository you specified for your fork, type
git remote -vagain. You should see the URL for your fork asoriginand the URL for the upstream repository asupstream.$ git remote -v > origin https://{% data variables.product.product_url %}/YOUR-USERNAME/YOUR-FORK.git (fetch) > origin https://{% data variables.product.product_url %}/YOUR-USERNAME/YOUR-FORK.git (push) > upstream https://{% data variables.product.product_url %}/ORIGINAL-OWNER/ORIGINAL-REPOSITORY.git (fetch) > upstream https://{% data variables.product.product_url %}/ORIGINAL-OWNER/ORIGINAL-REPOSITORY.git (push)
You can now keep your fork synced with the upstream repository with a few Git commands. See AUTOTITLE.
{% endwebui %}
{% cli %}
{% data reusables.cli.cli-learn-more %}
To configure a remote repository for the forked repository, use the --remote flag.
gh repo fork REPOSITORY --remote=trueTo specify the remote repository's name, use the --remote-name flag.
gh repo fork REPOSITORY --remote-name "main-remote-repo"{% endcli %}
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 submit a pull request to ask the original author to pull your fork into their repository. See AUTOTITLE.
Fork a repository to start contributing to a project. {% data reusables.repositories.you-can-fork %}
For more information about when you can fork a repository, see AUTOTITLE.
{% ifversion fpt or ghec %}You can browse Explore {% data variables.product.prodname_dotcom %} to find projects and start contributing to open source repositories. See AUTOTITLE.
{% endif %}
You have now forked a repository, practiced cloning your fork, and configured an upstream repository.
-
For more information about using Git on the command line to clone and sync changes, see AUTOTITLE.
-
You can also create a new repository to store your projects and share the code on {% data variables.product.prodname_dotcom %}. {% data reusables.getting-started.create-a-repository %}
-
{% data reusables.getting-started.being-social %}
-
{% data reusables.support.connect-in-the-forum-bootcamp %}
