What's the difference between cloning and forking a repository?
Show answer — try answering out loud first
Cloning (git clone) copies a repository to your local machine. Forking creates a copy of the repository in your own GitHub account, on the server. A fork is a GitHub feature for collaborating on projects where you don't have write access; cloning is a Git command for downloading any repo to your machine.
- Cloning: a Git command. It copies a remote repo to your computer so you can work locally. You stay connected to the original remote via
origin. It's useful when you do have write access (you're part of the team). - Forking: a GitHub feature. It creates a copy of the repo under your account on the server. It's the way to contribute to other people's projects (like open source) where you can't push directly. Afterward you clone your fork to your machine.
The typical flow for contributing to someone else's project:
- Fork the original repo to your account.
- Clone your fork to your machine.
- Make changes and push them to your fork.
- Open a Pull Request from the fork to the original repo.
# CLONE: download a repo to your machine (you need the URL)
git clone https://github.com/usuario/proyecto.git
# The "origin" remote points to the original repo
# FORK: done from the GitHub interface ("Fork" button),
# or with GitHub's official CLI:
gh repo fork usuario/proyecto --clone
# Creates the copy in your account AND clones it in a single step
# Fork flow: keep an "upstream" remote pointing to the original
git clone https://github.com/TU-USUARIO/proyecto.git # your fork
cd proyecto
git remote add upstream https://github.com/usuario/proyecto.git
# origin -> your fork (where you push)
# upstream -> the original repo (where you pull updates from)
git fetch upstream
Thinking that "fork" and "clone" are the same thing. Cloning only downloads a copy to your machine; a fork creates a copy on the server, in your account. Another mistake, when contributing via a fork, is forgetting to add the upstream remote pointing to the original project and falling behind on the changes others make.
"Cloning is a Git command that copies a repo to my machine; I use it when I already have write access to the project. A fork is a GitHub feature that creates a copy of the repo in my own account on the server, and it's the way to contribute to other people's projects where I can't push directly. The typical open source flow is: I fork, clone my fork, make changes, push them to my fork, and open a PR to the original repo. I usually add an
upstreamremote pointing to the original to keep up to date."
You want to contribute to an open source project where you're not a collaborator (you don't have push access). Do you clone the original repo directly, or fork it first?
See answer
You fork it first. Since you don't have write access to the original repo, you wouldn't be able to push your changes there. The fork creates a copy in your account that you can push to; then you clone your fork, work, push to your fork, and open a Pull Request to the original project so the maintainers can review and integrate your changes.