GitHub and remotes
So far, everything has lived on your computer. A remote is a copy of your repository hosted somewhere else, usually GitHub, GitLab or Bitbucket. Remotes let you back up your work, share it, and collaborate.
The in-browser simulator has no network, so this lesson uses real commands to run in your own terminal.
The big picture
Your laptop GitHub Teammate's laptop
┌─────────────┐ git push ┌──────────────┐ git pull ┌─────────────┐
│ local repo │ ────────────▶ │ origin (repo)│ ────────────▶ │ local repo │
│ │ ◀──────────── │ │ ◀──────────── │ │
└─────────────┘ git pull └──────────────┘ git push └─────────────┘originis the conventional name for your main remote.git pushuploads your commits.git fetchdownloads new commits without changing your files.git pull=fetch+ merge (or rebase) into your current branch.
Set up authentication (once)
GitHub no longer accepts account passwords for Git. Use either:
Option A: SSH keys (recommended)
ssh-keygen -t ed25519 -C "you@example.com" # press Enter to accept the defaults
cat ~/.ssh/id_ed25519.pub # copy this public key
# GitHub → Settings → SSH and GPG keys → New SSH key → paste
ssh -T git@github.com # test it: "Hi username! You've successfully authenticated"Option B: HTTPS with the GitHub CLI
gh auth loginPush an existing project to GitHub
- On GitHub, click New repository and give it a name. Don't add a README if you already have one locally.
- Connect and push:
git remote add origin git@github.com:your-username/recipe-book.git
git branch -M main
git push -u origin main-u (--set-upstream) links your local main to origin/main, so from then on you can just type git push and git pull.
git remote -v # list remotes and their URLsClone an existing repository
git clone git@github.com:facebook/react.git
cd react
git log --oneline -5Cloning downloads the entire history. The remote is automatically named origin.
Daily collaboration loop
git switch main
git pull # 1. get the latest changes
git switch -c feature/search # 2. branch off
# ... edit, add, commit ...
git push -u origin feature/search # 3. publish your branch
# 4. open a Pull Request on GitHub (next lessons)Remote-tracking branches
After a fetch, Git keeps read-only copies of the remote's branches, like origin/main:
git fetch
git status # "Your branch is behind 'origin/main' by 3 commits"
git log --oneline main..origin/main # what's new on the remote
git branch -a # list local and remote branchesWhen a push is rejected
! [rejected] main -> main (fetch first)
error: failed to push some refsSomeone pushed before you, so your history is missing their commits. Fix it by integrating their work first:
git pull --rebase # replay your commits on top of theirs (cleaner history)
git push🚫 Never
git push --forceon a shared branch likemain. It overwrites teammates' work. If you must force-push your own feature branch after a rebase, usegit push --force-with-lease, which refuses if someone else has pushed in the meantime.
Forks: contributing to other people's projects
You can't push to repositories you don't own. Instead:
- Fork the repo on GitHub (this creates your own copy).
- Clone your fork, branch, commit, and push to your fork.
- Open a Pull Request from your fork to the original ("upstream") repository.
git remote add upstream git@github.com:original-owner/project.git
git fetch upstream
git rebase upstream/main # keep your fork up to dateREADME and GitHub profile
Every repository should have a README.md explaining what the project is, how to run it, and how to contribute. GitHub renders it on the repo's home page. Your GitHub profile, with good READMEs and green contribution squares, is a real part of a developer portfolio.
Practice (real terminal)
- Create a free GitHub account if you don't have one, and set up SSH.
- Create a local repo with a
README.md, commit it, and push it to a new GitHub repository. - Edit the README on GitHub's website (pencil icon), commit there, then run
git pulllocally and confirm the change arrived.
1.What does git pull do?
2.Your push is rejected because the remote has commits you don't have. What should you do?