In this lab, you will pair up with another student to practice the GitHub fork and pull request workflow. This is the standard workflow for contributing to open source projects or any project where you don’t have write access to the repository.
Prerequisites
- You should have completed Lab 2 and have a repository on GitHub with some content
- Exchange GitHub repo and usernames with your lab partner
Lab objectives
In this lab you will:
- Fork your partner’s repository
- Clone it to your local machine
- Add the upstream remote
- Create a new branch
- Add a new file or make changes to an existing file
- Commit the changes
- Push the changes to your fork
- Submit a pull request to the original repository
- Review and merge your partner’s pull request
Step 1 - Fork your partner’s repository
- Browse to your partner’s repository on GitHub:
https://github.com/<partner-username>/<inits>GitFun - Select the Fork button in the top right corner
- In the Choose an owner dropdown, select your personal account
- Select the Create fork button
GitHub creates a copy of your partner’s repository in your account.
Step 2 - Clone your fork to your local machine
In your terminal, navigate to your Git directory:
cd ~/GitClone your fork:
git clone https://github.com/<your-username>/<repository-name>.gitAlternatively, you can use the GitHub CLI:
gh repo clone <your-username>/<repository-name>Change to the repository directory:
cd <repository-name>
Step 3 - Add the upstream remote
This step links your local repository to the original repository (your partner’s repo). This allows you to pull changes from the original repository to keep your fork in sync. If you cloned the fork using the GitHub CLI, the upstream remote is added automatically. If you cloned using Git, you need to add the upstream remote manually:
git remote add upstream https://github.com/<partner-username>/<repository-name>.git
Verify the remotes:
git remote -v
You should see both origin (your fork) and upstream (your partner’s original repo).
Step 4 - Create a working branch
Always create a new branch for your changes. Never work directly on the main branch.
git checkout -b add-my-contribution
Step 5 - Make changes
Using Visual Studio Code, make some changes to the repository. Here are some ideas:
- Add a new file called
contributors.mdand add your name - Add a new section to the README
- Create a new file with some content related to the project
- Fix a typo or improve existing documentation
Step 6 - Stage and commit your changes
git add -A
git commit -m "Add my contribution to the project"
Step 7 - Push your working branch to your fork
git push origin add-my-contribution
Step 8 - Submit a pull request
- Go to your partner’s original repository on GitHub:
https://github.com/<partner-username>/<repository-name>/pulls - Select the New pull request button
- Select compare across forks
- Set up the pull request:
- Base repository:
<partner-username>/<repository-name>base:main - Head repository:
<your-username>/<repository-name>compare:add-my-contribution
- Base repository:
- Fill out the pull request description:
- Add a title (e.g., “Add contributions from [your name]”)
- Add a description of what you changed and why
- Select Create pull request
Step 9 - Review your partner’s pull request
Now your partner should have submitted a pull request to YOUR repository. Let’s review and merge it:
- Go to your repository on GitHub
- Select on the Pull requests tab
- Select on your partner’s pull request
- Review the changes:
- Look at the Files changed tab to see what was modified
- Add comments if you have feedback
- Select Review changes and select Approve
- Select Merge pull request
- Select Confirm merge
- Optionally, delete the branch after merging
Step 10 - Sync your local repository
After the PR is merged, update your local repository:
git checkout main
git pull origin main
Step 11 - Cleanup your working branch
Delete the branch you used for your contribution:
git push origin --delete add-my-contribution
git branch -D add-my-contribution
Bonus: Keep your fork in sync
As the original repository (upstream) receives changes, you’ll want to keep your fork updated:
git checkout main
git pull upstream main
git push origin main