Fork, Clone, Push — Your First Contribution
The Fork-and-Contribute Model
Most repositories you encounter on GitHub — popular open source projects, company tools you want to improve, documentation sites with errors — are repositories you do not have push access to. You cannot directly create branches, push commits, or open pull requests against the original. The fork-and-contribute model is how collaboration works in this context.
The model has four phases:
- Fork — create your own copy of the repository under your GitHub account.
- Clone — download your fork to your local machine for development.
- Contribute — make your changes in a branch on your fork.
- Propose — open a pull request from your fork's branch to the original repository's main branch.
This model is the foundation of essentially all open source contribution and is also used within organizations for cross-team contributions to repositories you don't own. Understanding it deeply means you can contribute to any project on GitHub.
Forking a Repository
Navigate to any repository on GitHub and click the Fork button in the top-right area of the repository header. GitHub will ask you where to fork the repository (your personal account or one of your organizations) and whether to copy all branches or just the default branch.
What forking creates:
- A complete copy of the repository under your account:
github.com/yourusername/reponame - A
forkrelationship tracked by GitHub — your fork knows it was forked from the original (called the upstream) - Your own independent copy of all branches, commits, issues settings, and releases
- A link back to the original repository displayed on your fork's GitHub page
Importantly, forking is a GitHub concept, not a Git concept. The fork relationship is metadata tracked by GitHub. From Git's perspective, it is just another remote repository.
What forking does NOT do:
- It does not automatically keep your fork in sync with the upstream as it receives new commits.
- It does not give you write access to the original repository.
- It does not affect the original repository in any way.
Forking with the gh CLI
Cloning Your Fork vs Cloning the Original
After forking, you have two options for cloning:
Cloning Your Fork (Correct Approach for Contributing)
After cloning your fork, the origin remote points to your fork. This is correct — you push your branches to your fork, not directly to the upstream.
Cloning the Original Directly
If you clone the original repository directly, you have read access but cannot push branches to it. This is appropriate for tools you use but don't intend to contribute to. For contributions, always clone your fork.
Setting Up the Upstream Remote
After cloning your fork, you have one remote: origin pointing to your fork. To keep your fork in sync with the original repository, you need to add a second remote called upstream pointing to the original.
The naming convention upstream is not enforced by Git — it is just a convention followed by essentially every developer. Use it consistently.
If you forked with gh repo fork --clone, the CLI sets up the upstream remote automatically.
Making Changes and Pushing to Your Fork
The workflow for making a contribution:
Step 1: Create a Branch
Never commit directly to main on your fork. Create a descriptive feature branch for each contribution:
Branch naming conventions vary by project. Many projects document their conventions in CONTRIBUTING.md. Common patterns:
fix/description— bug fixesfeat/description— new featuresdocs/description— documentation changeschore/description— maintenance tasks (dependency updates, CI changes)refactor/description— code restructuring without behavior changes
Step 2: Make Your Changes
Edit files, write code, run tests. Follow the project's code style. Many projects have a CONTRIBUTING.md that explains how to set up a development environment, run tests, and format code.
Step 3: Push Your Branch to Your Fork
After pushing, GitHub will usually display a banner in the repository page offering to create a pull request for the recently pushed branch.
Keeping Your Fork in Sync
The upstream repository continues receiving commits after you fork it. If you work on a branch over several days or weeks, your fork's main can fall significantly behind the upstream's main. Keeping your fork in sync prevents painful merge conflicts later.
The Sync Workflow
Syncing via GitHub's UI
GitHub provides a Sync fork button on your fork's main page. Click it to bring your fork's default branch up to date with the upstream. This is a convenient shortcut but does not update your local clone — you still need to git pull afterwards.
Rebasing Your Feature Branch on Updated Main
If you synced main after you created your feature branch, you may want to rebase your branch on the updated main to incorporate new changes and reduce merge conflicts:
Opening a Pull Request from a Fork
Once your branch is pushed to your fork, you can open a pull request proposing to merge your changes into the upstream repository.
Via GitHub UI
- Go to the upstream repository (not your fork) on GitHub.
- GitHub will often show a banner: "Your branch
fix/my-featureinyourusername/reponamehad recent pushes. Compare & pull request." Click it. - If no banner appears, click the Pull requests tab, then New pull request, then click compare across forks.
- Set:
- base repository: the original (upstream) repository
- base branch: the branch you want to merge into (usually
main) - head repository: your fork
- compare branch: your feature branch
Via gh CLI
Pull requests are covered in full detail in lesson 3. For now, know that the PR sends your proposed changes to the upstream maintainers for review.
The First Open Source Contribution Workflow — End to End
Here is the complete workflow from zero to merged contribution, using a real example: fixing a documentation typo in an open source project.
At this point the maintainers will receive a notification. They may:
- Approve and merge immediately (for trivial fixes)
- Ask for changes (respond to their comments, push new commits to your branch — the PR updates automatically)
- Close without merging (if they disagree with the change — read their feedback carefully)
Reading the CONTRIBUTING.md
Before submitting your first contribution to any project, read the CONTRIBUTING.md file in the root of the repository. It typically covers:
- How to set up the development environment
- Code style requirements (linters, formatters)
- Test requirements (all PRs must include tests)
- PR title and description format (some projects use Conventional Commits)
- How to get help (Discord, Slack, Discussions)
Ignoring the CONTRIBUTING.md and submitting a PR that violates project conventions wastes everyone's time. Read it first.
gh CLI Shortcuts for the Fork/Clone/PR Workflow
The gh CLI makes the fork-clone-upstream setup a single command:
Listing and Managing Your Forks
Common Mistakes and How to Avoid Them
Committing Directly to main on Your Fork
Always work in branches. If you commit to main on your fork, synchronizing with upstream becomes complicated because your main now has commits that don't exist upstream.
Opening a PR Against the Wrong Base
When opening a PR from a fork, double-check that the base repository and base branch are what you intend. GitHub sometimes auto-populates the wrong base — especially if the upstream has a development branch.
Forgetting to Sync Before Starting New Work
Always run git fetch upstream && git merge upstream/main before starting a new feature branch. Starting from stale main means your eventual PR will have more conflicts.
Practical Exercises
Exercise 1 — Fork and Clone
- Navigate to
github.com/firstcontributions/first-contributions— a repository designed specifically for first-time contributors. - Fork the repository.
- Clone your fork.
- Add the upstream remote.
- Verify both remotes with
git remote -v.
Exercise 2 — Make a Contribution
- Follow the contribution instructions in
first-contributions' README. - Create a branch named
add-yourname. - Add your name to the
Contributors.mdfile. - Commit and push to your fork.
- Open a pull request.
Exercise 3 — Sync Your Fork
- Find a repository that has been recently updated (check the commit history).
- Fork it.
- Wait a day and then practice the sync workflow:
git fetch upstream,git merge upstream/main,git push origin main. - Use the GitHub UI's Sync fork button and then
git pullto bring your local clone up to date.
Exercise 4 — Using gh for the Full Workflow
- Pick a repository on GitHub with a simple
good first issuelabel. - Use
gh repo fork --clone --remoteto set up your fork in one command. - Create a branch, make a change, push it.
- Use
gh pr createto open the pull request from the terminal.