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
| Fork | Clone | |
|---|---|---|
| Creates a copy | On GitHub (your account) | On your machine |
| Independence | Separate from original | Linked to original |
| Use case | Contribute to projects you don't own | Work on your own projects |
| Connection | upstream → original | origin → remote |
The Complete Fork Workflow
Step 1: Fork on GitHub
- Go to the target repository on GitHub
- Click the Fork button (top right)
- Choose your account
- 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
| Practice | Description |
|---|---|
| Read CONTRIBUTING.md | Every project has its own rules |
| Look at existing issues | Check if the bug/feature is already tracked |
| Open an issue first | For large changes, discuss before coding |
| Small, focused PRs | One change per PR = easier review |
| Write tests | Most projects require tests for new code |
| Follow the style | Use the same formatting as the project |
| Respond to feedback | PRs can take time and iterations |
| Be patient | Maintainers 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
| Command | Description |
|---|---|
gh repo fork <repo> | Fork on GitHub |
git clone <url> | Clone a repository |
git remote add upstream <url> | Add original reference |
git fetch upstream | Fetch from original |
git merge upstream/main | Sync with original |
gh pr create | Create a Pull Request |
Next Steps
- Hands-on Lab TP4 — Complete GitHub workflow
- Module 04 - Collaboration & Pull Requests