Forking and Pull Requests on GitHub
Forking creates a personal copy of someone else's GitHub repository into a GitHub account. The fork is completely independent — changes made to the fork do not affect the original repository. However, the connection to the original project is maintained, making it easy to contribute back.
Forking is the foundation of open-source contribution. Instead of asking the project owner for direct write access, anyone can fork the project, make changes in their own copy, and then submit those changes back as a Pull Request.
Real-life analogy: Think of a fork as photocopying a recipe book. The photocopy can be marked up, pages can be changed, and notes can be added without touching the original. If the recipe is improved, a suggestion can be sent back to the original author.
How to Fork a Repository
- Go to the GitHub repository to fork
- Click the "Fork" button in the top-right corner of the repository page
- Select the account to fork into
- GitHub creates a copy of the repository in the account
The forked repository will now appear at: https://github.com/your-username/original-repo-name
Fork → Clone → Modify → Push Workflow
# Step 1: Fork the repo on GitHub (done via browser)
# Step 2: Clone the forked repo to local machine
git clone https://github.com/myusername/project.git
cd project
# Step 3: Add the original repo as "upstream" remote
git remote add upstream https://github.com/originalauthor/project.git
# Step 4: Create a new branch for the changes
git checkout -b fix-typo-in-readme
# Step 5: Make the changes
# ... edit files ...
# Step 6: Commit and push to the fork
git add .
git commit -m "Fix typo in README"
git push origin fix-typo-in-readme
# Step 7: Go to GitHub and open a Pull Request
Keeping a Fork Updated
When the original repository gets new commits, the fork will be behind. The fork can be updated by pulling from the upstream:
# Fetch changes from the original project
git fetch upstream
# Switch to the main branch
git checkout main
# Merge upstream changes into local main
git merge upstream/main
# Push the updated main to the fork
git push origin main
What is a Pull Request?
A Pull Request (PR) is a formal way to propose changes to a repository on GitHub. Instead of directly merging code, a PR creates a discussion space where the project maintainer and other contributors can:
- Review the code changes line by line
- Leave comments and suggestions
- Request modifications
- Approve and merge the changes when satisfied
Pull Requests are used in two main scenarios:
- Between branches — When working in a team and changes from a feature branch need to be merged into
main - Between forks — When contributing to someone else's open-source project
Creating a Pull Request on GitHub
- Push the branch with changes to GitHub
- Go to the repository on GitHub
- GitHub usually shows a yellow banner saying "Your branch had recent pushes" — click "Compare & pull request"
- Or go to the "Pull requests" tab and click "New pull request"
- Select the base branch (where the changes will go, e.g.,
main) and the compare branch (the branch with changes) - Write a title and description for the PR:
- Explain what was changed and why
- Reference any related issues (e.g., "Fixes #42")
- Add screenshots if relevant (for UI changes)
- Click "Create pull request"
Writing a Good Pull Request Description
A well-written PR description speeds up the review process:
## What does this PR do?
Fixes the login button that was unresponsive on mobile devices.
## How was it tested?
Tested on iPhone 12 (Safari) and Android Chrome.
## Related Issue
Fixes #27
## Screenshots
[Before screenshot] [After screenshot]
Reviewing a Pull Request
As a reviewer, GitHub shows a "Files changed" tab with all the line-by-line differences. Reviewers can:
- Click on a specific line to add a comment
- Approve the PR (it is ready to merge)
- Request changes (modifications are needed before merging)
- Simply comment without a formal decision
Merging a Pull Request
Once the PR is approved, it can be merged using one of three methods:
| Method | Description | When to Use |
|---|---|---|
| Merge Commit | Creates a merge commit — preserves all commits from the branch | General use — keeps full history |
| Squash and Merge | Combines all branch commits into a single commit | When the branch has many small/messy commits |
| Rebase and Merge | Replays commits on top of the base branch | For a clean, linear history |
Closing a Pull Request Without Merging
If a PR is no longer needed or was submitted by mistake, it can be closed without merging by clicking "Close pull request" at the bottom of the PR page.
Draft Pull Requests
A Draft PR signals that the work is still in progress and should not be merged yet. It allows early feedback and discussion. To create a draft PR, click the dropdown arrow next to "Create pull request" and select "Create draft pull request".
Summary
Forking creates a personal copy of someone else's repository to work on freely. A Pull Request is a proposal to merge changes into a project. PRs provide a space for code review, discussion, and quality control before changes go into the main codebase. Together, forking and PRs form the backbone of open-source collaboration on GitHub.
