GitHub Fork
Clone, fork, and add upstream remotes so your copy can receive updates without overwriting someone else's default branch.
A fork is GitHub, not git clone
GitHub Repository created a hosted repository you own. A fork is another GitHub repository: GitHub copies someone else's project into your account. git clone is a Git command that copies a URL onto disk. You can clone without forking. You can fork and not clone yet.
Fork
A hosted copy under YOU/REPO. It has its own clone URL, issues later, and default branch. GitHub created it.
Clone
A working tree plus object database on your machine. origin is whatever URL you cloned. That URL should be your fork.
Create the hosted copy, then clone it
# GitHub copies the repository into YOUR namespace, then clones that copy
gh repo fork ORIGINAL/REPO --clone
cd REPO
git remote -v
# Typical result after gh repo fork --clone:
# origin git@github.com:YOU/REPO.git
# upstream git@github.com:ORIGINAL/REPO.gitgh repo fork ORIGINAL/REPO --clone asks GitHub to copy the repository, then clones the copy. Many setups also add upstream automatically. The website Fork button does the hosted copy only; you still clone the URL under your username.
# Website Fork button, then clone YOUR fork (not the original)
git clone git@github.com:YOU/REPO.git
cd REPO
git remote add upstream git@github.com:ORIGINAL/REPO.git
git remote -v
git fetch upstreamUse a public repository you do not own. Do not fork a private employer project without permission. You can delete a practice fork from GitHub settings when the lab is done.
Test what you learned
Type the GitHub CLI command that forks ORIGINAL/REPO and clones the copy (include --clone).
origin is yours; upstream is theirs
Git Remote taught that a remote is a name plus a URL. A fork clone usually needs two:
origin
Your GitHub fork. git push origin main updates the copy you control.
upstream
The original GitHub repository. git fetch upstream updates your picture of their default branch.
Inspect
git remote -v must show two different URLs. Same URL twice means you have not separated the jobs.
Credentials
Fetch and push still use the HTTPS or SSH method from GitHub Authentication.
Test what you learned
Type the command that adds a remote named upstream pointing at git@github.com:ORIGINAL/REPO.git.
Fetch upstream before you change files
git fetch upstream downloads objects and updates remote-tracking names such as upstream/main. It does not merge into your current branch. Read git log --oneline --decorate --graph --all and git log --oneline main..upstream/main before you integrate. That is the same fetch-then-inspect habit you used with two local clones.
Test what you learned
Type the command that downloads objects from upstream without merging them.
Sync by integrating, then pushing origin
git switch main
git fetch upstream
git log --oneline --decorate --graph --all
git merge upstream/main
git push origin mainMerge upstream/main into your local main when you intend to catch up. Then git push origin main publishes that catch-up on your fork. GitHub’s “Sync fork” button does a hosted version of the same idea; you should still be able to name the remotes and the Git commands.
If you already committed on main of the fork, integrating upstream can produce a merge commit or a conflict. Resolve it the way Git Merge Conflicts taught. Do not force-push main to make the graph pretty unless you are sure nobody else cloned that fork.
Do not push someone else's default branch
# This updates someone else's default branch if you have permission.
# You almost never want it, and GitHub usually rejects it.
git push upstream mainMaintainers publish on upstream. Your write path is origin, usually a feature branch, and later a pull request. Permission denied on git push upstream main is GitHub protecting the original default branch—not a broken SSH key. Do not force-push to “fix” it.
Guided practice: two remotes on one clone
- 01Fork a public repository you do not own
Use
gh repo forkor the Fork button. Confirm the GitHub URL contains your username. - 02Clone your fork
If the CLI did not clone,
git clonethe fork URL. Printgit remote -v. - 03Add upstream if missing
git remote add upstreampointing at the original. Fetch it. - 04Compare tips
git log --oneline main..upstream/main. Integrate only if you mean to catch up, thengit push origin main. - 05Refuse the wrong push
Do not run
git push upstream main. Write one sentence naming which remote you are allowed to update.
Independent lab: fork report
- Fork a public GitHub repository you do not own. Clone your fork, not the original URL as
origin. - Show
git remote -vwithoriginpointing at your namespace andupstreampointing at the original. - Run
git fetch upstreamand record whetherupstream/mainmatches your localmain. - If you are behind, merge
upstream/mainandgit push origin main. Do not push toupstream. - Write four lines: original URL, fork URL, the fetch command, and which remote you will never force-push.
Common fork mistakes
Calling clone a fork
Clone is Git. Fork is GitHub copying a hosted repository into your account.
origin still the original
Push will aim at a project you do not own. Clone the fork URL.
Pushing upstream main
That is their default branch. Push origin. Pull requests come later.
Treating Sync fork as magic
The button still fetch-and-integrates. You should be able to do it with remotes.
Lesson review
You can fork on GitHub, clone your copy, keep origin and upstream straight, and sync without overwriting someone else's default branch. The next lesson tracks work with GitHub Issues.
- I know a fork is a GitHub-hosted copy, not
git clone. - I can show
originas my fork andupstreamas the original withgit remote -v. - I can
git fetch upstream, integrate, andgit push origin main. - I do not push or force-push the original project's default branch.