6. Forking & Pull Requests

The **Pull Request (PR)** is the absolute heart of collaboration on GitHub. It is a feature that allows a developer to notify team members that they have completed a feature and would like their changes reviewed and merged into the main project. In this chapter, we will learn about the **Forking Workflow** and how to open Pull Requests.

1. What is Forking?

A **Fork** is a copy of a repository that is created in your own personal GitHub account. Forking is commonly used in open-source development because it allows you to make changes to a project without affecting the original repository directly.

To fork a repository:

  1. Go to the original repository's page on GitHub.
  2. Click the "Fork" button in the top-right corner.
  3. Select your account space. GitHub will create a complete copy of the project under your profile.

2. The Forking Workflow Sequence

To contribute to a repository using a fork, follow this standard pattern:

# 1. Clone your personal fork
git clone git@github.com:your-username/original-repo.git

# 2. Add the original repository as a second remote called "upstream"
cd original-repo
git remote add upstream git@github.com:original-organization/original-repo.git

# 3. Create a feature branch
git switch -c feature/my-new-idea

# 4. Make changes, stage, and commit
git add .
git commit -m "Implement my awesome new feature"

# 5. Push changes to YOUR fork (origin)
git push -u origin feature/my-new-idea

3. Opening a Pull Request

Once you push your feature branch to your fork, visit the original repository page on GitHub. GitHub will automatically detect that you recently pushed a branch and show a yellow banner:"Compare & pull request".

  1. Click the "Compare & pull request" button.
  2. Configure the branches:
    • Base Repository: The original project branch you want to merge into (usually original:main).
    • Head Repository: Your fork and feature branch (usually your-username:feature/my-new-idea).
  3. Write a descriptive **Title** and **Description** explaining exactly *what* you changed and *why*.
  4. Click the green "Create pull request" button.

4. Draft Pull Requests

If your code is still a Work-In-Progress (WIP) but you want to share it with your team for feedback or to run continuous integration tests, you can create a **Draft Pull Request**:

  • Click the drop-down arrow next to "Create pull request" and select "Create draft pull request".
  • Draft PRs cannot be merged until you click **"Ready for review"**, indicating the feature is finished.
Pro Tip: You can link an issue directly to a Pull Request by typing Closes #12 or Fixes #45in the PR description field. When the PR is merged, GitHub will automatically close the referenced issue for you!