Pular para o conteúdo principal

Fork & Clone — Contributing to Open Source

Module 03 45 min

Section Objectives

  • Understand the difference between fork and clone
  • Fork a project and make a contribution
  • Configure upstream to stay synchronized with the original
  • Navigate the open-source contribution process

Fork vs Clone

ForkClone
Creates a copyOn GitHub (your account)On your machine
IndependenceSeparate from originalLinked to original
Use caseContribute to projects you don't ownWork on your own projects
Connectionupstream → originalorigin → remote

The Complete Fork Workflow

Step 1: Fork on GitHub

  1. Go to the target repository on GitHub
  2. Click the Fork button (top right)
  3. Choose your account
  4. GitHub creates a copy at https://github.com/YOUR_USERNAME/project

Or with GitHub CLI:

gh repo fork facebook/react --clone
# Forks AND clones in one command

Step 2: Clone Your Fork

# Clone your fork (not the original)
git clone git@github.com:YOUR_USERNAME/react.git
cd react

# View configured remotes
git remote -v
# origin git@github.com:YOUR_USERNAME/react.git (fetch)
# origin git@github.com:YOUR_USERNAME/react.git (push)

Step 3: Add the Upstream Remote

# Add reference to the original project
git remote add upstream git@github.com:facebook/react.git

# Verify
git remote -v
# origin git@github.com:YOUR_USERNAME/react.git (fetch)
# origin git@github.com:YOUR_USERNAME/react.git (push)
# upstream git@github.com:facebook/react.git (fetch)
# upstream git@github.com:facebook/react.git (push)

Step 4: Create a Feature Branch

# Always work on a branch (NEVER on main of your fork)
git switch -c fix/typo-in-readme

# Make your changes
# ...

git add .
git commit -m "docs: fix typo in README installation section"

Step 5: Stay Synchronized with Upstream

# Fetch latest changes from original
git fetch upstream

# Update your main
git switch main
git merge upstream/main

# Update your feature branch (optional, but recommended)
git switch fix/typo-in-readme
git rebase upstream/main

Step 6: Push to Your Fork

git push -u origin fix/typo-in-readme

Step 7: Create a Pull Request

# Via GitHub CLI
gh pr create \
--repo facebook/react \
--title "docs: fix typo in README" \
--body "Fixed a typo in the installation section of README.md"

# Or manually on GitHub:
# Go to the original repo → Pull Requests → New Pull Request

git clone Options

# Standard clone
git clone git@github.com:username/repo.git

# Clone into a specific folder
git clone git@github.com:username/repo.git my-folder

# Clone with HTTPS (if SSH not configured)
git clone https://github.com/username/repo.git

# Shallow clone (only last N commits — faster for large repos)
git clone --depth 1 git@github.com:username/repo.git

# Clone a specific branch
git clone --branch develop git@github.com:username/repo.git

# Clone without checking out files (bare clone)
git clone --bare git@github.com:username/repo.git

Open Source Contribution Best Practices

PracticeDescription
Read CONTRIBUTING.mdEvery project has its own rules
Look at existing issuesCheck if the bug/feature is already tracked
Open an issue firstFor large changes, discuss before coding
Small, focused PRsOne change per PR = easier review
Write testsMost projects require tests for new code
Follow the styleUse the same formatting as the project
Respond to feedbackPRs can take time and iterations
Be patientMaintainers are often volunteers

Reading CONTRIBUTING.md

Most serious projects have a CONTRIBUTING.md file that describes:

# Contributing Guide

## Development Setup
1. Fork the repository
2. Clone: `git clone ...`
3. Install dependencies: `npm install`
4. Run tests: `npm test`

## Commit Convention
We use Conventional Commits:
- feat: new feature
- fix: bug fix
- docs: documentation

## Pull Request Process
1. Create a branch: `git switch -c type/description`
2. Make your changes
3. Run tests: `npm test`
4. Push and create PR
5. Wait for CI to pass
6. Request review from a maintainer

## Code of Conduct
Please read our Code of Conduct before contributing.

Summary

CommandDescription
gh repo fork <repo>Fork on GitHub
git clone <url>Clone a repository
git remote add upstream <url>Add original reference
git fetch upstreamFetch from original
git merge upstream/mainSync with original
gh pr createCreate a Pull Request

Next Steps