How to fork a Git repository

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


In the context of Git and GitHub, forking a repository means creating a copy of the repository in your GitHub account. This copy is independent of the original (upstream) repository, allowing you to make changes, experiment, or contribute back to the original project without directly modifying it.

This guide explains the concept of forking, its significance, and provides a step-by-step approach to forking repositories using GitHub as the primary example.

  • Repository (repo): A directory or storage space where your projects can live. It can be local to a folder on your computer, or it can be a storage space on GitHub or another online host.
  • Upstream repository: The original repository from which you've forked.
  • Fork: A copy of a repository that you manage on your account.
  • Clone: The act of copying a repo from GitHub to your local machine.
  • Contributing to open source projects: Forking is the first step to contributing to a project you don't have sole maintainer rights over. You can make changes in your fork and submit a pull request to the original repository.
  • Experimenting with changes: Forking allows you to freely experiment with changes without affecting the original project.
  • Diverging from the original project: Forking can be useful if you want to build upon existing code, but significantly divert the project away from the core maintainers' vision.

The easiest way to fork a repository is by using the GitHub web interface:

  1. Navigate to the GitHub repository: Go to the GitHub page of the repository you wish to fork.

  2. Fork the repository: Click the "Fork" button located at the top right of the page. GitHub will create a copy of the repository in your account.

GitHub fork button screenshot

While there's no direct git fork command in the Git CLI, GitHub provides a CLI tool that offers this functionality.

  1. Install GitHub CLI: Ensure you have the GitHub CLI installed on your system. Installation instructions can be found in the GitHub CLI documentation.

  2. Fork the repository: Use the gh repo fork command to fork a repository. For example:

    Terminal
    gh repo fork owner/repo

    This will create a fork of the repo from the owner's account into your GitHub account.

After forking, clone the forked repository to your local machine to start working on it:

Terminal
git clone https://github.com/yourusername/reponame.git

To keep your fork up to date with the upstream repository, you need to add the original repository as a remote source:

  1. Navigate to your repository's directory: Open a terminal and change to your repository's directory.

    Terminal
    cd reponame
  2. Add the upstream repository: Use the git remote add command to add the original repository as an upstream remote.

    Terminal
    git remote add upstream https://github.com/originalowner/reponame.git
  3. Fetch upstream changes: Fetch the branches and their respective commits from the upstream repository.

    Terminal
    git fetch upstream
  4. Merge changes into your fork: Merge the changes from the upstream's default branch (usually main or master) into your local default branch.

    Terminal
    git merge upstream/main
  5. Push updates to your fork: Finally, push the updated local commits to your forked repository on GitHub.

    Terminal
    git push origin main
  • Keep your fork up to date: Regularly sync your fork with the upstream repository to avoid merge conflicts.
  • Contribute back: Consider contributing your improvements back to the original project through pull requests.

For further reading on forking in Git, see the official GitHub documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2