Skip to content

Latest commit

 

History

History
329 lines (238 loc) · 6.82 KB

File metadata and controls

329 lines (238 loc) · 6.82 KB

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

  1. Navigate to the repository
  2. Click "Fork" button (top right)
  3. Choose where to fork (your account or organization)
  4. 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

  1. Go to your fork on GitHub
  2. Click "Compare & pull request" button
  3. Fill in PR details:
    • Title: Clear, concise description
    • Description: What, why, and how
    • Reference issues if applicable
  4. 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:

  1. When creating PR, click dropdown on "Create pull request"
  2. Select "Create draft pull request"
  3. 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

  1. GitHub Explore: https://github.com/explore
  2. First Timers Only: https://www.firsttimersonly.com/
  3. Good First Issue: Search GitHub
  4. Up For Grabs: https://up-for-grabs.net/

Before Contributing

  1. Read CONTRIBUTING.md
  2. Check open issues
  3. Look at existing PRs
  4. Understand code of conduct
  5. Set up development environment

Your First PR

Start small:

  • Fix typos
  • Improve documentation
  • Add tests
  • Fix obvious bugs

Building Reputation

  1. Start with documentation
  2. Report good bugs
  3. Help others in issues
  4. Submit quality PRs
  5. Be patient and respectful

Common Mistakes to Avoid

  1. Not syncing fork: Always update before creating PR
  2. Too many changes: Keep PRs focused
  3. No description: Explain what and why
  4. Force push to main: Never do this
  5. Arguing with maintainers: Be respectful
  6. 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