Forking: Creating Your Own Copy of the Repository

forking is a central concept in the world of code versioning with Git and code hosting platforms like GitHub. When you fork a repository, you are creating a personal copy of someone else's repository in your own GitHub account. This copy is independent of the original repository and serves as a starting point for you to make your own changes without affecting the original project.

Why Fork?

Forking a repository can have several motivations. The most common is when you want to contribute to a project but don't have permission to push directly to the original repository. When you create a fork, you get a copy that you can work on freely. After making your changes, you can propose that they be integrated into the original project via a pull request.

Another reason to fork is when you want to use the project as a starting point to develop a new feature or even a new project. The fork allows you to inherit the entire development history up to that point and continue from there.

How to Fork GitHub

To fork a repository on GitHub, you need to have an account on the platform. Once logged in, follow these steps:

  1. Navigate to the page of the repository you want to fork.
  2. In the top right corner of the page, click the Fork button.
  3. If you belong to multiple organizations, select where you want to fork.
  4. GitHub will start the forking process, and within a few seconds, you will be redirected to the page for your new fork.

Congratulations! You now have a copy of the repository in your GitHub account.

Managing Your Fork

After creating a fork, it is important to manage it correctly. This includes keeping your fork in sync with the original repository, which is essential to avoid conflicts when you try to contribute your changes.

To sync your fork, you need to set up a remote pointing to the original repository. This can be done as follows:

git remote add upstream https://github.com/original_owner/original_repository.git

With remote upstream configured, you can fetch the latest changes from the original repository using:

git fetch upstream

You can then merge these changes into your main branch (usually called master or main) with:

git checkout main
git merge upstream/main

If you have local changes that have not yet been committed to your fork on GitHub, you may need to resolve conflicts before completing the merge.

Contributing with Pull Requests

After you make your changes to your fork, you may want them to be included in the original repository. To do this, you must open a pull request. Here are the basic steps:

  1. Push your changes to your fork on GitHub.
  2. Navigate to your fork's GitHub page.
  3. Click the New Pull Request button.
  4. Review the changes being proposed and ensure they are correct.
  5. If everything is ok, click Create Pull Request.
  6. Give a title and detailed description for your pull request and submit.

Now, the original repository maintainers can review your pull request. They can accept it, request changes, or reject it.

Good Practices When Forking

When forking a repository, it is important to follow some good practices:

  • Respect the project license: Before forking, make sure you understand the license under which the project is distributed and respect its terms.
  • Keep your fork up to date: Regularly sync your fork with the original repository to avoid conflicts.
  • Contribute back: If you make significant improvements to your fork, consider contributing back to the original project.

Conclusion

Forking is a powerful tool that enables collaboration and innovation in software development. By creating your own copy of a repository, you can experiment, contribute, and even bring new projects to life. Remember to manage your fork carefully and contribute back to the community whenever possible.

With this knowledge, you are ready to fork projects on GitHub and start collaborating in the wide world of open source software development.

Now answer the exercise about the content:

What is the purpose of forking a repository on GitHub?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Fetch and Pull: updating local repositories 34

Next page of the Free Ebook:

Fetch and Pull: updating local repositories

Estimated reading time: 5 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text