Git and GitHub · Part 3 · GitHub and remote repositories

Cloning and forking

Working on someone else's code

So far, every repository started on the reader's own computer. Most real work starts the other way round: the code already exists on GitHub, in a team's repository or an open-source project. Getting a copy is cloning. Getting a copy of something without permission to push to it takes forking first.

Clone vs fork

CloneFork
What it makesA copy on the computerA copy on GitHub, under your account
Done withgit cloneThe Fork button
Used whenYou can push to the repository (your own, or your team's)You can't push to it (someone else's project)
Changes go back byPushing a branch, then a pull requestPushing to the fork, then a pull request to the original

A fork isn't a Git feature, it's GitHub's: a server-side clone that remembers where it came from. People still clone their fork to work on it.

Cloning

On a repository's GitHub page, click Code, choose SSH and copy the URL:

Terminal
cd ~/Desktop
git clone git@github.com:jknithin36/git-learning.git
cd git-learning

The clone has the full history, every branch as an origin/ bookmark, and main already set to track origin/main, so git pull and git push work right away.

CommandWhat it does
git clone url my-folderClones into a folder with a different name
git clone --depth 1 urlA shallow clone: only the latest commit, fast for huge repositories
gh repo clone owner/repoThe same with the GitHub CLI, without copying a URL

Contributing with a fork

The usual way to change an open-source project, from start to pull request:

1.Fork it on GitHub

On the project's page, click Fork, then Create fork. GitHub makes jknithin36/project, a copy under the reader's account that they can push to.

2.Clone the fork

Terminal
git clone git@github.com:jknithin36/project.git
cd project

3.Add the original as upstream

origin is the fork. The original project is conventionally called upstream:

Terminal
git remote add upstream git@github.com:owner/project.git
git remote -v
Output
origin    git@github.com:jknithin36/project.git (fetch)
origin    git@github.com:jknithin36/project.git (push)
upstream  git@github.com:owner/project.git (fetch)
upstream  git@github.com:owner/project.git (push)

upstream

owner/project

fork

origin

jknithin36/project

fetch upstream · push origin

Local

The clone on the laptop

4.Work on a branch

Terminal
git switch -c fix/typo-in-readme
# edit, then:
git commit -am "Fix typo in README"
git push -u origin fix/typo-in-readme

Keeping the fork's main untouched makes syncing simple, and one branch per change makes one pull request per change.

5.Open the pull request

GitHub shows a Compare & pull request banner on the fork. The pull request goes from jknithin36/project: fix/typo-in-readme into owner/project: main, and the project's maintainers review it there.

Keeping a fork in sync

The original project keeps moving, and the fork doesn't update itself. To bring the fork's main up to date:

Terminal
git switch main
git fetch upstream
git merge upstream/main
git push origin main
Output
From github.com:owner/project
 * [new branch]      main       -> upstream/main
Updating 525e85e..4253151
Fast-forward

Because the fork's main has no commits of its own, the merge is always a fast-forward. Then a feature branch can be brought up to date with git rebase main.

Two shortcuts do the same for main:

  • The Sync fork button on the fork's GitHub page, then git pull locally.
  • gh repo sync from the terminal. gh repo fork owner/project --clone also forks, clones and adds upstream in one go.

Quick reference

TaskCommand
Clone a repositorygit clone url
Add the original projectgit remote add upstream url
List remotesgit remote -v
Sync the fork's maingit fetch upstream, then git merge upstream/main
Fork, clone and add upstreamgh repo fork owner/project --clone
Sync with the CLIgh repo sync