GitHub Forking

A GitHub fork is a copy of a repository that resides in your account, allowing you to make changes independently of the original repository, and propose those changes back to the original project through a pull request.

“Forking” is a key concept in open-source development which is supported by GitHub, one of the most popular platforms for version control and collaboration on software development projects.

Here’s how it works:

  1. Forking a Repository: When you “fork” a repository, you’re creating a copy of the original repository on your own GitHub account. This lets you freely experiment with changes without affecting the original project. It’s akin to creating a branch, but in this case, the branch is entirely under your own control and doesn’t affect the original repository unless you want it to.

  2. Making Changes: Once you have forked a repository, you can make changes to your copy just like you would with any other repository. You can modify files, create, delete, or merge branches, and commit changes. These changes only affect your fork of the repository and do not affect the original repository.

  3. Creating a Pull Request: If you’ve made changes to your fork that you believe would be beneficial to the original repository, you can create a pull request. A pull request is a proposal to the maintainer of the original repository to merge your changes into the original repository. The maintainer can then review your changes and decide whether or not to accept them.

  4. Syncing a Fork: Over time, the original repository may be updated with changes that are not reflected in your fork. In such cases, you may want to sync your fork with the original repository. This involves fetching the changes from the original repository and merging them into your fork.

Forking is a common practice in open-source development because it allows anyone to contribute to a project, no matter how large or small their contribution might be. It provides a way for many people to work on a project simultaneously without stepping on each other’s toes or creating conflicts. Once their contributions are complete and tested, they can be integrated back into the main project through pull requests.

Step by step example of forking a repository

Walk through the process of forking a repository on GitHub:

Step 1: Find the Repository to Fork

First, navigate to the GitHub page of the repository you want to fork.

Step 2: Fork the Repository

In the top-right corner of the page, you will see a button labeled “Fork”. Click on this button. GitHub will then create a copy of this repository in your account. The name of the repository will be the same, but it will be prefixed with your username (i.e., username/repository-name).

Step 3: Clone the Forked Repository

Now that you’ve forked the repository, you’ll want to clone it to your local machine so that you can make changes. Navigate to your GitHub account, find the forked repository, and click on the “Code” button to find the URL to clone.

You can clone it by using this command in your terminal:

git clone git@github.com:your-username/repository-name.git

Remember to replace “your-username” and “repository-name” with your GitHub username and the name of the repository you’ve just forked.

Step 4: Make Changes to the Repository

Once the repository is on your local machine, you can make changes to it. Use your preferred text editor to modify files, then save your changes.

Step 5: Commit Your Changes

Once you’ve made all of your changes, you’ll want to commit them to the repository. You can do this by using the following commands:

git add .
git commit -m "Describe the changes you made"

Step 6: Push Your Changes

After committing your changes, you’ll want to push them to GitHub. You can do this with the following command:

git push origin master

Note: Replace ‘master’ with the name of the branch where you want to push your changes if it’s different.

Step 7: Create a Pull Request

Once your changes are pushed, you can create a pull request to the original repository. Go back to your GitHub account, navigate to the forked repository, and click on “Pull request”. On the new page, click on “New pull request”. You can then fill out the form, describing the changes you’ve made before clicking “Create pull request”.

The owner of the original repository will be notified of your pull request and will review your proposed changes before deciding whether to merge them into the original repository.

Last modified July 21, 2024: update (e2ae86c)