Git & GitHub · 11 min · 120 XP

Remotes: clone, push and pull

Put your repository on GitHub, and keep your copy and everyone else's in step.

A remote is another copy of your repository, usually on GitHub, and by convention it's called origin. Nothing moves between your copy and the remote on its own: push sends your new commits up, and pull brings other people's commits down and merges them into your branch.

Two ways to start
# A project that already exists on GitHub: download it, full history included
git clone https://github.com/acme/shop.git

# A repository you started locally: create an empty repo on GitHub, then
git remote add origin https://github.com/you/shop.git
git push -u origin main   # -u remembers origin/main, so next time it's just `git push`

Your first push asks you to sign in, and your GitHub password won't work: GitHub stopped accepting passwords for git in 2021. If git opens a browser to sign in (Git for Windows does), follow it. Otherwise install the GitHub CLI from cli.github.com and run gh auth login once. Choose HTTPS and let it set up git, and every push after that just works.

If your first push fails like this
$ git push -u origin main
error: src refspec main does not match any

# Your branch is called master (git's old default). Rename it, then push:
$ git branch -m main
$ git push -u origin main

git fetch downloads new commits from the remote but leaves your branch alone. After a fetch, git status tells you how far apart you are. git pull is a fetch followed by a merge (that's the pull.rebase false you set up in the first lesson), and it's what you'll usually run.

If a teammate pushed since you last pulled, GitHub rejects your push, because accepting it would throw their commits away. The fix is always the same: git pull, resolve a conflict if there is one, then push again. git push --force would get your push through by deleting their work from GitHub. Don't use it on a shared branch.

Scenario: you try to push
$ git push
To github.com:acme/shop.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:acme/shop.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.
Scenario: after git fetch
$ git status
On branch main
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.

Try it on your machine: create an empty repository on GitHub (no README), connect your practice repository with git remote add origin …, sign in with gh auth login if git asks you to, and git push -u origin main. Refresh the GitHub page and your commits are there. Then edit a file on GitHub's website, commit it there, and bring it back with git pull.

Loading your workspace…