Skip to main content

Fork a repo

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

About forks

Most commonly, forks are used to either propose changes to someone else's project to which you do not have write access, or to use someone else's project as a starting point for your own idea. You can fork a repository to create a copy of the repository and make changes without affecting the upstream repository. 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 enterprise, see GitHub's white paper "An introduction to innersource."

Prerequisites

If you have not yet, you should first set up Git. Don't forget to set up authentication to your enterprise from Git as well.

Forking a repository

You might fork a project to propose changes to the upstream, or original, 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 enterprise, navigate to the octocat/Spoon-Knife repository.
  2. In the top-right corner of the page, click Fork. Fork button

Para saber mais sobre a GitHub CLI, confira "Sobre a 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 enterprise, navigate to your fork of the Spoon-Knife repository.

  2. Acima da lista de arquivos, clique em Código. Botão "Código"

  3. Copie a URL do repositório.

    • Para clonar o repositório usando HTTPS, em "HTTPS", clique em .
    • Para clonar o repositório usando uma chave SSH, incluindo um certificado emitido pela autoridade de certificação SSH da sua organização, clique em SSH e em .
    • Para clonar um repositório usando GitHub CLI, clique em GitHub CLI e selecione . O ícone da área de transferência para copiar a URL para clonar um repositório com o CLI do GitHub
  4. Abra TerminalTerminalGit Bash.

  5. Altere o diretório de trabalho atual para o local em que deseja ter o diretório clonado.

  6. Type git clone, and then paste the URL you copied earlier. It will look like this, with your GitHub AE 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.

Para saber mais sobre a GitHub CLI, confira "Sobre a GitHub CLI".

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

gh repo fork REPOSITORY --clone=true
  1. No menu Arquivo, clique em Clonar Repositório.

    Opção de menu Clone (Clonar) no app Mac

    Opção de menu Clone (Clonar) no app Windows

  2. Clique na aba que corresponde à localização do repositório que você deseja clonar. Clique também na URL para inserir o local do repositório manualmente.

    Guias Location (Local) no menu Clone a repository (Clonar um repositório)

    Guias Location (Local) no menu Clone a repository (Clonar um repositório)

  3. Escolha o repositório que você quer clonar na lista.

    Clonar uma lista de repositórios

    Clonar uma lista de repositórios

  4. Clique em Escolher… e procure um caminho local em que deseja clonar o repositório.

    Botão Choose (Escolher)

    Botão Choose (Escolher)

  5. Clique em Clonar.

    Botão Clone (Clonar)

    Botão Clone (Clonar)

Configuring Git to sync your fork with the original repository

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

  1. On your enterprise, navigate to the octocat/Spoon-Knife repository.

  2. Acima da lista de arquivos, clique em Código. Botão "Código"

  3. Copie a URL do repositório.

    • Para clonar o repositório usando HTTPS, em "HTTPS", clique em .
    • Para clonar o repositório usando uma chave SSH, incluindo um certificado emitido pela autoridade de certificação SSH da sua organização, clique em SSH e em .
    • Para clonar um repositório usando GitHub CLI, clique em GitHub CLI e selecione . O ícone da área de transferência para copiar a URL para clonar um repositório com o CLI do GitHub
  4. Abra TerminalTerminalGit 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 original 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."

Para saber mais sobre a GitHub CLI, confira "Sobre a 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 are hoping to contribute back to the original 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. If the policies for your enterprise permit forking private and internal repositories, you can fork a repository to your personal account or an organization where you have repository creation permissions. For more information, see "Roles in an organization."

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. A criação de um repositório para seu projeto permite que você armazene código no GitHub. Isso fornece um backup do seu trabalho que você pode optar por compartilhar com outros desenvolvedores. Para obter mais informações, confira "Criar um repositório"."

  • Cada repositório em GitHub pertence a uma pessoa ou organização. Você pode interagir com as pessoas, repositórios e organizações, conectando-se e seguindo-as em GitHub AE. Para obter mais informações, confira "Seja social".

  • O GitHub tem uma ótima comunidade de suporte na qual você pode pedir ajuda e conversar com pessoas de todo o mundo. Participe da conversa na GitHub Community.