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
| Clone | Fork | |
|---|---|---|
| What it makes | A copy on the computer | A copy on GitHub, under your account |
| Done with | git clone | The Fork button |
| Used when | You can push to the repository (your own, or your team's) | You can't push to it (someone else's project) |
| Changes go back by | Pushing a branch, then a pull request | Pushing 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:
cd ~/Desktop
git clone git@github.com:jknithin36/git-learning.git
cd git-learningThe 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.
| Command | What it does |
|---|---|
git clone url my-folder | Clones into a folder with a different name |
git clone --depth 1 url | A shallow clone: only the latest commit, fast for huge repositories |
gh repo clone owner/repo | The 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
git clone git@github.com:jknithin36/project.git
cd project3.Add the original as upstream
origin is the fork. The original project is conventionally called upstream:
git remote add upstream git@github.com:owner/project.git
git remote -vorigin 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
origin
jknithin36/project
Local
The clone on the laptop
4.Work on a branch
git switch -c fix/typo-in-readme
# edit, then:
git commit -am "Fix typo in README"
git push -u origin fix/typo-in-readmeKeeping 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:
git switch main
git fetch upstream
git merge upstream/main
git push origin mainFrom github.com:owner/project
* [new branch] main -> upstream/main
Updating 525e85e..4253151
Fast-forwardBecause 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 pulllocally. gh repo syncfrom the terminal.gh repo fork owner/project --clonealso forks, clones and addsupstreamin one go.
Quick reference
| Task | Command |
|---|---|
| Clone a repository | git clone url |
| Add the original project | git remote add upstream url |
| List remotes | git remote -v |
| Sync the fork's main | git fetch upstream, then git merge upstream/main |
| Fork, clone and add upstream | gh repo fork owner/project --clone |
| Sync with the CLI | gh repo sync |