How to Fork a Repo in Git

The open-source community thrives because developers earning six-figure annual salaries still contribute to projects that many other developers can benefit from for free. GitHub enables this spirit of collaboration by giving users the ability to fork public repositories, make contributions, and send pull requests to the project administrators.

What is Forking in Git?

Forking is the act of copying a public repository from another user’s GitHub account to your own. The moment you fork a repository, that forked copy becomes your own, meaning that you can do all that you would do with a repository you created yourself. You can edit the repository’s contents online, clone it to your computer, or make new commits without affecting the parent repository.

Cloning a repository is similar to forking; however, a significant difference is that cloning only copies the repository to your local work environment. A cloned repository has no link to its parent repository, and to perform a remote commit, you will need to create a new repository on your account. When you fork a repository, there is still a link to the parent repository even if the forked copy now technically belongs to you.

After making whatever changes you have made on your forked copy, you can request the addition of your changes to the original work. You will need to send a pull request to the author of the original copy; once they review and accept your input, it’ll get merged as a new commit to their work. An accepted pull request will usually show the author’s name who created the pull request in the commit log.

When a team of developers works on a project, every member will have read and write access to the repository. Forking allows users who don’t have write access to contribute to a project without the risk of novices breaking the project or unauthorized additions. It also prevents the workflow from becoming rowdy with multiple branches that are hard to track.

How To Fork A Repo

Forking a repository can be easily done by following the steps below:

  • First, create an account on GitHub or log in if you already have an account.
  • Search for the repository that you want to clone on GitHub. For this example, I will use a different account to fork a repository I created for a git pull article; the repository’s name is git pull.
  • Once you locate the repository, click the Fork button at the top of the screen; beside the button is a number indicating how many times GitHub users have forked the repo. 
Fork Repo Git
The fork button on GitHub
  • The forking process will take a few seconds to round up, after which you will see the repository on your account, allowing you to work with it as you wish.
    There will be an indication at the top showing the location of the source repository.
forked repository on a contributor's account
A forked repo on another account
  • You can see the timeline of changes made on a forked repository on the Network Graph in the Insights tab.
Network graph showing contributors on a project
Network graph showing contributors on a project

Trivia: At the time of writing, the most forked repository on GitHub is Tensorflow, an open-source Machine Learning framework.

How to Send a Pull Request

Except you have express permission to use a forked repository for your personal use, it’s ethically wrong and, in some cases, illegal to do so. Often, you’ll fork a repository to contribute to the project. When you have made changes on your fork, you will need to send a pull request to the author of the repository so that they can merge it with the original work.

It is called a pull request and NOT a push request because it is not the contributor trying to push an update (because they do not have the right to do so). Instead, the contributor is suggesting that the owner of the repository performs a git pull consisting of the changes that they have made to the repository.

The steps to send a pull request from a fork are as follows:

  • After making your changes, click the Pull requests tab and select the New pull request button.
Create new pull request
  • The next screen that comes up will be for you to compare the changes you’ve made and the original branch, informing you of any conflicts between your update and the original. If all is okay, you should click the green Create pull request button.
compare changes and create new pull request
  • You can add a comment to the pull request explaining the contents of your update to the repository owner or administrators before sending the pull request.

Accepting A Pull Request

After a contributor sends a pull request, an administrator needs to approve the changes to merge it with the main repository. The steps to accept a pull request are as follows:

  • If you’re the repository owner or an administrator, click on the Pull requests tab; the tab will display a list of pending pull requests.
  • Select the one you want to accept and click the Merge pull request button. It’s always nice to add a complimentary comment before doing so.
  • If you don’t want to accept the pull request, you can Close the request (with a comment if you want to). 
    After a pull request is accepted, that update will be the latest commit on that branch of the parent repository.
Network chart reflecting the source of a merge
Network chart reflecting the source of a merge

Read More: How To Clone A Git Repository

Leave a Comment