SovranCode
HomeCourses Git & GitHub GitHub Fork
This device
Course contentsGitHub Fork · 41 topics

1. Git Fundamentals

Git IntroductionGit Version ControlGit vs GitHubGit InstallationGit Working TreeGit RepositoryGit CommitGit .gitignoreGit Project: Learning Journal

2. Git History

Git LogGit DiffGit ResetGit RevertGit StashGit Project: Messy Repository

3. Branches & History

Git BranchGit MergeGit Merge ConflictsGit RebaseGit Cherry-PickGit TagsGit Project: Feature Branch

4. Remotes and GitHub

Git RemoteGitHub AuthenticationGitHub RepositoryGitHub ForkGitHub IssuesGitHub PagesGit Project: Two Clone Sync

5. GitHub Collaboration

GitHub Pull RequestsGitHub Code ReviewGit Branching WorkflowGitHub Branch ProtectionGitHub CollaborationGitHub ContributingGit Project: Reviewed Pull Request

6. Automation and Professional Git

GitHub ActionsGit InternalsGit RecoveryGitHub SecurityGit Project: Team Repository CIProject
Learn Git & GitHub40 complete · 1 planned

1. Git Fundamentals

Git IntroductionGit Version ControlGit vs GitHubGit InstallationGit Working TreeGit RepositoryGit CommitGit .gitignoreGit Project: Learning Journal

2. Git History

Git LogGit DiffGit ResetGit RevertGit StashGit Project: Messy Repository

3. Branches & History

Git BranchGit MergeGit Merge ConflictsGit RebaseGit Cherry-PickGit TagsGit Project: Feature Branch

4. Remotes and GitHub

Git RemoteGitHub AuthenticationGitHub RepositoryGitHub ForkGitHub IssuesGitHub PagesGit Project: Two Clone Sync

5. GitHub Collaboration

GitHub Pull RequestsGitHub Code ReviewGit Branching WorkflowGitHub Branch ProtectionGitHub CollaborationGitHub ContributingGit Project: Reviewed Pull Request

6. Automation and Professional Git

GitHub ActionsGit InternalsGit RecoveryGitHub SecurityGit Project: Team Repository CIProject
PREVIOUS LESSONGitHub Repository
NEXT LESSONGitHub Issues
4. Remotes and GitHub 75 min

GitHub Fork

Clone, fork, and add upstream remotes so your copy can receive updates without overwriting someone else's default branch.

What you will leave with

You will fork a GitHub repository into your namespace, clone that copy, name origin and upstream, and bring new commits from the original without pushing onto someone else's default branch. Opening a pull request is the next collaboration lesson.

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.

GITHUB

Fork

A hosted copy under YOU/REPO. It has its own clone URL, issues later, and default branch. GitHub created it.

GIT

Clone

A working tree plus object database on your machine. origin is whatever URL you cloned. That URL should be your fork.

Do not clone the original as origin if you intend to push

If origin is still ORIGINAL/REPO, git push aims at a repository you do not own. Fork first, then clone your GitHub copy.

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.git

gh 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 upstream

Use 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.

QUICK CHECK

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.

QUICK CHECK

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.

QUICK CHECK

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 main

Merge 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 main

Maintainers 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.

Contribution is not this lesson

A fork is the GitHub copy. Returning a change through review is GitHub Pull Requests. Stop when origin and upstream are named, fetch works, and you can sync without touching their main.

Guided practice: two remotes on one clone

  1. 01
    Fork a public repository you do not own

    Use gh repo fork or the Fork button. Confirm the GitHub URL contains your username.

  2. 02
    Clone your fork

    If the CLI did not clone, git clone the fork URL. Print git remote -v.

  3. 03
    Add upstream if missing

    git remote add upstream pointing at the original. Fetch it.

  4. 04
    Compare tips

    git log --oneline main..upstream/main. Integrate only if you mean to catch up, then git push origin main.

  5. 05
    Refuse 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

  1. Fork a public GitHub repository you do not own. Clone your fork, not the original URL as origin.
  2. Show git remote -v with origin pointing at your namespace and upstream pointing at the original.
  3. Run git fetch upstream and record whether upstream/main matches your local main.
  4. If you are behind, merge upstream/main and git push origin main. Do not push to upstream.
  5. Write four lines: original URL, fork URL, the fetch command, and which remote you will never force-push.
Definition of done

You can explain fork versus clone, show two remotes with different URLs, fetch upstream without merging by accident, and update origin without touching the original default branch.

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 origin as my fork and upstream as the original with git remote -v.
  • I can git fetch upstream, integrate, and git push origin main.
  • I do not push or force-push the original project's default branch.

Related lessons

  • GitHub Repository — A fork is a GitHub copy, not a Git command.
  • Git Remote — origin and upstream are remotes with different jobs.
  • GitHub Authentication — Clone and fetch need a working credential.
  • GitHub Issues — A fork has its own Issues tab; do not file noise on the original.
  • GitHub Pull Requests — A change from a fork returns as a pull request, not a push to upstream main.
  • GitHub Contributing — Read the project's guide before you open that pull request.
KNOWLEDGE CHECK

Check your fork and remotes model

Separate GitHub fork from git clone, name origin versus upstream, and sync without pushing onto someone else's default branch.

01What is a GitHub fork?
02On a typical fork clone, what should origin and upstream point at?
03Does git clone create a GitHub fork?
04How should you bring new commits from the original project into your fork?
05Why must you not git push upstream main as a first habit?
06Where does a fork live before you clone it?
07After you sync, which remote should show your new default-branch commit?
PREVIOUS LESSONGitHub Repository
NEXT LESSONGitHub Issues
ON THIS PAGEGitHub ForkA fork is GitHub, not git cloneCreate the hosted copy, then clone itorigin is yours; upstream is theirsFetch upstream before you change filesSync by integrating, then pushing originDo not push someone else's default branchGuided practice: two remotes on one cloneIndependent lab: fork reportCommon fork mistakesLesson reviewKnowledge checkRelated lessons
Course contents