How to Fork a Repo in GitHub
This article outlines the process of creating and managing forks on GitHub. A fork is a copy of a repository you manage on your GitHub account.
Forks allow us to make changes to a project without necessarily altering the original repository. We can update forks by fetching changes or suggesting changes to the original owner through a fork.
Fork a Repo in GitHub
When we create a fork on GitHub, we can:
- Send a pull request to the original owner suggesting changes to the original repository.
- Synchronize our fork to update with the changes from the original repository, usually referred to as
upstream
.
Propose Changes to Someone’s Project
A perfect scenario to use a fork is when you find a bug in someone’s code. Instead of logging the issue, you can simply:
- Fork the repo.
- Fix the bug.
- Submit a pull request to the owner.
Use a Project as a Starting Point for Your Idea
We can also use another person’s project as a starting point for our project. A good example is when developing an API framework.
Let’s take the example of REST APIs. There are dozens of boilerplates/templates projects on GitHub.
Most of these are public repositories and allow you to fork and start making changes.
How do you fork on GitHub?
Fork a Repository
Once you have found a repository to fork, follow these simple steps, and you will be up and running in no time.
-
On GitHub.com, open the repository you would like to fork, navigate to the top-right corner, and click
Fork
. -
Choose an owner for the fork and give it a name; you can also add a short description of the fork. Additionally, you can copy only the default branch.
-
Once satisfied, click
Create Fork
to finish the process. This will create a copy of the repository in your account.
After creating a fork, you need to clone the fork to your machine to start making changes. Cloning the fork will bring the files to your local repository in your machine.
-
Navigate to the fork on GitHub and click on
Code
. -
For this session, we will clone the repo using HTTPS. Copy the URL under HTTPS.
-
We will open the Git Bash and switch to the working directory we want to clone. Use the
git clone
command with the link you copied to clone the repo.
All the files and branches from the fork will be copied to your local machine, and you can start developing.
It is always advisable to keep your fork up-to-date with the original repository. We can configure git to sync our fork with the original repository by following these steps:
-
On GitHub, navigate to the original repository and click
Code
. (Identical with the previous session). -
We will copy the HTTPS URL and open the Git Bash.
-
Next, we will switch to the directory containing our clone of the fork and run the
git remote -v
command to see which remote is configured with our fork. -
We can now use the
git remote add upstream
command with our link, as shown below:$ git remote add upstream https://github.com/app-generator/flask-corona-dark.git
To sync the fork, we will fetch from the upstream by running the git fetch upstream
command.
We can now merge the changes from the upstream master
branch with our local fork’s master
branch with the git merge upstream/master
command. Before proceeding, ensure you are checked out to your local master
branch; master
in this case is our fork’s default branch.
If we had some unique commits in our local repository, we would have to deal with conflicts. If this is not the case, we will do a fast-forward merge.
This will update our master
branch with the upstream changes. What’s left is pushing the changes to our fork on GitHub.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn