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.
Key Terms:
- 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.
Why fork a repository?
- 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.
How to fork a repository
GitHub fork button
The easiest way to fork a repository is by using the GitHub web interface:
Navigate to the GitHub repository: Go to the GitHub page of the repository you wish to fork.
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.
Fork from the GitHub CLI
While there's no direct git fork
command in the Git CLI, GitHub provides a CLI tool that offers this functionality.
Install GitHub CLI: Ensure you have the GitHub CLI installed on your system. Installation instructions can be found in the GitHub CLI documentation.
Fork the repository: Use the
gh repo fork
command to fork a repository. For example:Terminalgh repo fork owner/repoThis will create a fork of the
repo
from theowner
's account into your GitHub account.
Cloning your fork
After forking, clone the forked repository to your local machine to start working on it:
git clone https://github.com/yourusername/reponame.git
Keeping your fork updated
To keep your fork up to date with the upstream repository, you need to add the original repository as a remote source:
Navigate to your repository's directory: Open a terminal and change to your repository's directory.
Terminalcd reponameAdd the upstream repository: Use the
git remote add
command to add the original repository as an upstream remote.Terminalgit remote add upstream https://github.com/originalowner/reponame.gitFetch upstream changes: Fetch the branches and their respective commits from the upstream repository.
Terminalgit fetch upstreamMerge changes into your fork: Merge the changes from the upstream's default branch (usually
main
ormaster
) into your local default branch.Terminalgit merge upstream/mainPush updates to your fork: Finally, push the updated local commits to your forked repository on GitHub.
Terminalgit push origin main
Forking workflow best practices
- 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.