Pull Requests & Forks
Understanding Forks, Pull Requests, and Their Workflow in GitHub
Pull Requests & Forks
When working on open-source projects or collaborating with others on GitHub, two of the most important concepts are Forks and Pull Requests (PRs).
They allow developers to contribute safely to projects without directly modifying the original repository.
What is a Fork?
A fork is your own copy of someone else’s repository.
It lives in your GitHub account and allows you to freely experiment, make changes, and contribute without affecting the original project.
Key Points About Forks:
- A fork is a server-side copy of the repository created on your GitHub account.
- It allows you to experiment without impacting the original repository.
- Once you’re ready with changes, you can propose them back to the original repository using a Pull Request (PR).
When to Use a Fork?
You typically fork a repository when:
- You want to contribute to someone else’s project.
- You don’t have write access to the original repository.
- You want your own copy of the project to experiment with.
How to Fork a Repository
- Go to the repository page on GitHub.
- Click the Fork button (top-right corner).
- GitHub creates a copy of the repo under your account.
Example:
- Original repo:
github.com/original-owner/project
- Your fork:
github.com/your-username/project
Cloning Your Fork Locally
After forking, clone the forked repo to your machine:
git clone https://github.com/your-username/project.git
cd project
Set the upstream remote to the original repository:
git remote add upstream https://github.com/original-owner/project.git
Check your remotes:
git remote -v
You should see:
origin
→ your forkupstream
→ the original repository
Keeping Your Fork Updated
Since the original repository (upstream) may receive new commits, you should sync your fork:
# Fetch changes from upstream
git fetch upstream
# Switch to your local main branch
git checkout main
# Merge changes into your local main
git merge upstream/main
Push changes to your fork:
git push origin main
Making Changes in Your Fork
Want to learn more about branching?
We've covered that topic in detail—check out Branching.
- Create a new branch for your changes:
git checkout -b feature-branch
- Make changes in the code and commit them:
git add .
git commit -m "Added a new feature"
- Push the branch to your fork:
git push origin feature-branch
What is a Pull Request (PR)?
A Pull Request (PR) is a way to propose changes from your fork (or branch) to the original repository. It lets the maintainers review your code before merging.
Key Points About PRs:
- PRs are opened on GitHub, not locally.
- They compare your branch (from your fork) with a branch in the original repo.
- PRs allow discussion, review, and approval before merging.
How to Create a Pull Request
- Push your branch to your fork (done earlier).
- Go to the original repository on GitHub.
- GitHub will prompt: “Compare & pull request” → click it.
- Add a title and description of your changes.
- Submit the Pull Request.
Pull Request Workflow
Here’s the typical workflow:
- Fork the repository.
- Clone your fork locally.
- Set upstream to the original repository.
- Create a new branch for your changes.
- Make changes and commit them.
- Push your branch to your fork.
- Open a PR from your fork → original repository.
- Discuss & fix (if maintainers request changes).
- PR gets merged by the maintainers.
Example
# Fork repo on GitHub
git clone https://github.com/your-username/project.git
cd project
git remote add upstream https://github.com/original-owner/project.git
# Sync fork
git fetch upstream
git checkout main
git merge upstream/main
# Work on a feature
git checkout -b feature-login
# (make changes)
git add .
git commit -m "Added login feature"
git push origin feature-login
Now open GitHub → Create Pull Request.
After Pull Request is Merged
Once your PR is merged into the original repository:
- You can delete the branch you created in your fork (to keep things clean).
- Keep your fork updated regularly with the upstream repository.
git checkout main
git fetch upstream
git merge upstream/main
git push origin main
Summary
- Fork → Makes a copy of a repo under your account.
- Pull Request → Proposes your changes back to the original repo.
- Workflow → Fork → Clone → Branch → Commit → Push → PR → Merge.
This process is the backbone of open-source collaboration.