Day 28: Understanding GitHub Forks and Pull Request Workflow
Forks and pull requests are fundamental to open source collaboration on GitHub. This article explains how to contribute to projects you don’t have write access to.
Understanding Forks
A fork is a complete copy of a repository under your GitHub account. It allows you to:
- Experiment freely without affecting the original project
- Propose changes via pull requests
- Customize the project for your needs
Fork vs Clone vs Branch
- Fork: Copy repository on GitHub (your account)
- Clone: Copy repository to local machine
- Branch: Create divergent line of development
Forking a Repository
On GitHub
- Navigate to the repository
- Click “Fork” button (top right)
- Choose where to fork (your account or organization)
- Wait for GitHub to create the fork
Cloning Your Fork
# Clone your fork
git clone https://github.com/your-username/repo-name.git
# Navigate to directory
cd repo-name
# Add upstream remote
git remote add upstream https://github.com/original-owner/repo-name.git
# Verify remotes
git remote -v
# origin https://github.com/your-username/repo-name.git (fetch)
# origin https://github.com/your-username/repo-name.git (push)
# upstream https://github.com/original-owner/repo-name.git (fetch)
# upstream https://github.com/original-owner/repo-name.git (push)
Complete Pull Request Workflow
Step 1: Sync Your Fork
# Fetch upstream changes
git fetch upstream
# Checkout your main branch
git checkout main
# Merge upstream changes
git merge upstream/main
# Push to your fork
git push origin main
Step 2: Create Feature Branch
# Create and checkout feature branch
git checkout -b fix-typo-in-readme
# Make your changes
# Edit files...
# Stage changes
git add README.md
# Commit changes
git commit -m "Fix typo in installation instructions"
Step 3: Push to Your Fork
# Push branch to your fork
git push origin fix-typo-in-readme
Step 4: Create Pull Request
- Go to your fork on GitHub
- Click “Compare & pull request” button
- Fill in PR details:
- Title: Clear, concise description
- Description: What, why, and how
- Reference issues if applicable
- Click “Create pull request”
Step 5: Respond to Feedback
# Make requested changes
# Edit files...
# Commit changes
git add .
git commit -m "Address review feedback"
# Push updates
git push origin fix-typo-in-readme
# PR automatically updates!
Step 6: After Merge
# Switch to main
git checkout main
# Pull merged changes
git pull upstream main
# Push to your fork
git push origin main
# Delete feature branch
git branch -d fix-typo-in-readme
git push origin --delete fix-typo-in-readme
Keeping Fork Updated
Regular Sync
# Fetch and merge in one command
git checkout main
git pull upstream main
git push origin main
Automated Sync
Use GitHub’s “Fetch upstream” button on your fork’s page, or set up GitHub Actions:
# .github/workflows/sync-upstream.yml
name: Sync Fork
on:
schedule:
- cron: '0 0 * * *' # Daily
workflow_dispatch:
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: TobKed/github-forks-sync-action@v1
with:
upstream: original-owner/repo-name
target: your-username/repo-name
Multiple Commits in One PR
Method 1: Interactive Rebase
# Squash commits
git rebase -i HEAD~3
# In editor, change 'pick' to 'squash' for commits to combine
# Save and edit commit message
# Force push (since you're rewriting history)
git push -f origin feature-branch
Method 2: Soft Reset
# Reset to before your commits
git reset --soft HEAD~3
# Create single commit
git commit -m "Complete feature implementation"
# Force push
git push -f origin feature-branch
Dealing with Merge Conflicts in PRs
# Sync with upstream
git fetch upstream
# Rebase on upstream/main
git rebase upstream/main
# Resolve conflicts
# Edit conflicting files
git add resolved-files
git rebase --continue
# Force push
git push -f origin feature-branch
Draft Pull Requests
Create draft PR for early feedback:
- When creating PR, click dropdown on “Create pull request”
- Select “Create draft pull request”
- Mark as ready when complete
Benefits:
- Get early feedback
- Show work in progress
- Prevents accidental merge
Pull Request Best Practices
Before Creating PR
- Code works and is tested
- Follows project style guidelines
- Documentation is updated
- Commits are clean and logical
- Branch is up to date with main
Writing Good PRs
Title:
❌ Fix stuff
✅ Fix null pointer exception in user login
Description:
## What
Fixes a null pointer exception that occurs when users login with empty passwords.
## Why
Bug #123 reported crashes on invalid login attempts.
## How
Added null check before password validation.
## Testing
- Tested with empty password
- Tested with valid password
- Tested with invalid password
Closes #123
During Review
- Respond to all comments
- Ask questions if unclear
- Make requested changes promptly
- Be open to suggestions
- Thank reviewers
Contributing to Open Source
Find Projects
- GitHub Explore: https://github.com/explore
- First Timers Only: https://www.firsttimersonly.com/
- Good First Issue: Search GitHub
- Up For Grabs: https://up-for-grabs.net/
Before Contributing
- Read
CONTRIBUTING.md - Check open issues
- Look at existing PRs
- Understand code of conduct
- Set up development environment
Your First PR
Start small:
- Fix typos
- Improve documentation
- Add tests
- Fix obvious bugs
Building Reputation
- Start with documentation
- Report good bugs
- Help others in issues
- Submit quality PRs
- Be patient and respectful
Common Mistakes to Avoid
- Not syncing fork: Always update before creating PR
- Too many changes: Keep PRs focused
- No description: Explain what and why
- Force push to main: Never do this
- Arguing with maintainers: Be respectful
- Not testing: Always test your changes
Today’s Summary
Forks and pull requests enable massive open source collaboration. Understanding this workflow opens up thousands of projects you can contribute to. Start small, be patient, and remember that every experienced contributor was once a beginner too.
Let me reorganize the Git commands and parameters learned today:
- git clone
- git remote add upstream
- git fetch upstream
- git merge upstream/main
- git push origin branch-name
- git push -f origin branch-name
- git rebase -i HEAD~n
- git reset –soft HEAD~n
- Back to Table of Contents
- Previous Day: Version Control Through Branches in the Same Remote Repository
- Next Day: How to Import Subversion Projects into Git Repository