Fork Workflow Workshop
Practice the complete fork workflow. This exercise simulates contributing to an open source project.
Overview
You'll:
- Fork a repository
- Clone your fork
- Set up upstream tracking
- Create a contribution
- Submit a pull request
Exercise 1: Fork and Clone
Option A: Using GitHub CLI
bash1# Fork and clone in one step2$ gh repo fork octocat/Spoon-Knife --clone3✓ Created fork your-username/Spoon-Knife4Cloning into 'Spoon-Knife'...56$ cd Spoon-Knife
Note: octocat/Spoon-Knife is GitHub's demo repository specifically for practicing forks.
Option B: Manual Process
- Go to https://github.com/octocat/Spoon-Knife
- Click "Fork" button
- Clone your fork:
bash1$ git clone git@github.com:your-username/Spoon-Knife.git2$ cd Spoon-Knife
Exercise 2: Configure Remotes
Check Current Remotes
bash1$ git remote -v2origin git@github.com:your-username/Spoon-Knife.git (fetch)3origin git@github.com:your-username/Spoon-Knife.git (push)
Add Upstream Remote
bash1$ git remote add upstream git@github.com:octocat/Spoon-Knife.git
Verify Both Remotes
bash1$ git remote -v2origin git@github.com:your-username/Spoon-Knife.git (fetch)3origin git@github.com:your-username/Spoon-Knife.git (push)4upstream git@github.com:octocat/Spoon-Knife.git (fetch)5upstream git@github.com:octocat/Spoon-Knife.git (push)
Exercise 3: Sync with Upstream
Fetch Upstream Changes
bash1$ git fetch upstream2From github.com:octocat/Spoon-Knife3 * [new branch] main -> upstream/main
Update Your Main Branch
bash1$ git checkout main2$ git rebase upstream/main3Current branch main is up to date.
Push to Your Fork
bash1$ git push origin main2Everything up-to-date
Exercise 4: Create a Contribution
Create a Feature Branch
bash1$ git checkout -b contribution/add-my-name2Switched to a new branch 'contribution/add-my-name'
Make Your Change
bash1# Add your name to a file2$ echo "- Your Name was here!" >> README.md
Commit the Change
bash1$ git add README.md2$ git commit -m "docs: add my name to contributors"3[contribution/add-my-name abc1234] docs: add my name to contributors4 1 file changed, 1 insertion(+)
Exercise 5: Push to Your Fork
bash1$ git push -u origin contribution/add-my-name2Enumerating objects: 5, done.3...4To github.com:your-username/Spoon-Knife.git5 * [new branch] contribution/add-my-name -> contribution/add-my-name6Branch 'contribution/add-my-name' set up to track remote branch 'contribution/add-my-name' from 'origin'.
Exercise 6: Create Pull Request
Using GitHub CLI
bash1$ gh pr create \2 --repo octocat/Spoon-Knife \3 --base main \4 --head your-username:contribution/add-my-name \5 --title "Add my name to README" \6 --body "Adding my name to practice the fork workflow."78Creating pull request for your-username:contribution/add-my-name into main in octocat/Spoon-Knife910https://github.com/octocat/Spoon-Knife/pull/12345
View Your PR
bash1$ gh pr view --repo octocat/Spoon-Knife
Note: PRs to Spoon-Knife won't actually be merged—it's just for practice.
Exercise 7: Simulate Upstream Update
Let's practice syncing when upstream changes:
Create a Fake Upstream Change
On your fork's main branch:
bash1$ git checkout main23# Simulate upstream having new content4$ echo "Upstream change" >> upstream-changes.txt5$ git add upstream-changes.txt6$ git commit -m "simulate: upstream change"7$ git push origin main
Make Your Branch Outdated
bash1$ git checkout contribution/add-my-name2# Your branch is now behind main
Sync Process
bash1# Fetch the latest2$ git fetch origin34# Rebase onto updated main5$ git rebase origin/main6First, rewinding head to replay your work on top of it...7Applying: docs: add my name to contributors89# Force push the rebased branch10$ git push --force-with-lease origin contribution/add-my-name
Exercise 8: Clean Up
Delete Local Branch
bash1$ git checkout main2$ git branch -d contribution/add-my-name
Delete Remote Branch
bash1$ git push origin --delete contribution/add-my-name
Optional: Delete Fork
If you want to delete the practice fork:
bash1$ gh repo delete your-username/Spoon-Knife --yes
Or delete via GitHub Settings → Delete repository.
Workflow Summary
bash1# One-time setup2$ gh repo fork org/project --clone3$ cd project4$ git remote add upstream git@github.com:org/project.git56# For each contribution7$ git fetch upstream8$ git checkout main9$ git rebase upstream/main10$ git push origin main11$ git checkout -b feature/my-change12# ... make changes ...13$ git add . && git commit -m "description"14$ git push -u origin feature/my-change15$ gh pr create --repo org/project1617# Keep PR updated18$ git fetch upstream19$ git rebase upstream/main20$ git push --force-with-lease
Checklist
- Forked a repository
- Cloned your fork locally
- Added
upstreamremote - Synced main with upstream
- Created a feature branch
- Pushed branch to your fork
- Created PR to upstream repository
- Practiced rebasing when upstream updates
Key Takeaway
The fork workflow enables contributions to projects you don't have direct access to. Always keep your fork synced with upstream, and create PRs from feature branches—never from main.