Fork: create your own server-side copy of someone else’s repository on a platform like GitHub
- so you can make changes safely and submit them back with a pull request.
Fork is mainly a GitHub feature, not a basic local Git command.
When you fork a repository, GitHub creates your own copy of someone else’s repository under your account.
Use a fork when:
- you do not have direct write access to the original repository
- you want to make changes in your own copy first
- you plan to open a Pull Request back to the original repository
Fork vs Clone
Fork:
- happens on GitHub
- creates a copy under your GitHub account
Clone:
- happens on your computer
- downloads a repository to your local machine
In a normal contribution flow, you usually:
- Fork on GitHub
- Clone your fork locally
- Make changes
- Push to your fork
- Open a Pull Request
1) Fork the repository on GitHub
- Open the original repository on GitHub.
- Click the
Forkbutton. - Choose your GitHub account as the destination.
- Wait for GitHub to create the fork.
After this, you will have a new repository like:
https://github.com/<your-username>/<repo-name>2) Clone your fork to your computer
Copy the URL of your fork, then run:
git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>3) Check the remote repository
git remote -vUsually, origin will point to your fork.
Example:
origin https://github.com/<your-username>/<repo-name>.git4) Add the original repository as upstream
This is recommended so you can pull later updates from the original project.
git remote add upstream https://github.com/<original-owner>/<repo-name>.gitCheck again:
git remote -vNow you will usually have:
origin= your forkupstream= the original repository
5) Create a new branch for your work
git checkout -b my-changeUse a short branch name that describes your work.
Examples:
fix-typoupdate-docsadd-fork-guide
6) Make changes, commit, and push
git status
git add .
git commit -m "Describe what you changed"
git push -u origin my-changeThis pushes your branch to your fork on GitHub.
7) Open a Pull Request
After pushing, go to your fork on GitHub.
GitHub will usually show a button like Compare & pull request.
Open the Pull Request from:
- your fork branch
to:
- the original repository
Keep Your Fork Updated
If the original repository changes, you may want to sync your fork.
First fetch updates from upstream:
git fetch upstreamThen update your local main branch:
git checkout main
git merge upstream/mainThen push the updated main branch to your fork:
git push origin mainNote: some repositories use master instead of main.
Quick Version
# Fork on GitHub first
git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>
git remote add upstream https://github.com/<original-owner>/<repo-name>.git
git checkout -b my-change
git add .
git commit -m "Describe what you changed"
git push -u origin my-changeCommon Problems
I cloned the original repository instead of my fork
You may not be able to push if you do not have write access.
Check:
git remote -vIf origin points to the original repository, that is the problem.
I get a permission error when pushing
You are probably trying to push to the original repository instead of your fork.
Check:
git remote -vMake sure:
originpoints to your forkupstreampoints to the original repository
My fork is behind the original repository
Sync from upstream:
git fetch upstream
git checkout main
git merge upstream/main
git push origin main