Remote Repositories

So far everything has lived on one computer. A remote is another copy of the repository — usually on a hosting service such as GitHub — that you send commits to and get commits from. It's your backup, and the meeting point where everyone on a team exchanges work.

How remotes fit in

TXT
   Sam's laptop                     GitHub                     Alex's laptop
  ┌────────────┐    git push     ┌──────────┐    git pull     ┌────────────┐
  │  recipes   │ ──────────────▶ │ recipes  │ ──────────────▶ │  recipes   │
  │ (full repo)│ ◀────────────── │  (bare)  │ ◀────────────── │ (full repo)│
  └────────────┘    git pull     └──────────┘    git push     └────────────┘

Every copy holds the complete history. Nobody works directly in the server's copy; people commit locally and then push their new commits up, or pull other people's commits down. Nothing is exchanged until you run one of those commands.

Creating a repository on GitHub

To publish an existing local project:

  1. Sign in to GitHub, click the + menu at the top right and choose New repository.
  2. Enter a name (it doesn't have to match your folder, but it usually does), and choose Public or Private.
  3. Leave "Add a README", ".gitignore" and "license" unticked when you're uploading an existing project — otherwise GitHub creates a first commit that your local history doesn't have.
  4. Click Create repository. GitHub shows the repository's URL and the commands to connect it.

GitLab, Bitbucket and others work the same way. The commands below are identical for all of them; only the URL changes.

Adding a remote

Back in the terminal, inside the project, give the remote a name and its URL. By convention the main remote is called origin:

$
git remote add origin https://github.com/sam-rivera/recipes.git
git remote -v
Output
origin	https://github.com/sam-rivera/recipes.git (fetch)
origin	https://github.com/sam-rivera/recipes.git (push)

-v shows the URL used to fetch and to push; they're almost always the same. The name is just a local nickname — origin saves you typing the URL every time.

The first push

Send your main branch to origin. -u also links your local main to the remote one, so later you can simply type git push and git pull:

$
git push -u origin main
Output
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 10 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 520 bytes | 520.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/sam-rivera/recipes.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

(GitHub may ask you to sign in the first time — lesson 31 covers authentication.) Refresh the repository page in your browser and your files and commit history are there. Pushing is covered fully in lesson 32.

Inspecting a remote

$
git remote show origin
Output
* remote origin
  Fetch URL: https://github.com/sam-rivera/recipes.git
  Push  URL: https://github.com/sam-rivera/recipes.git
  HEAD branch: main
  Remote branch:
    main tracked
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (up to date)

This contacts the server and reports which branches exist there and how your local branches relate to them.

Managing remotes

CommandDoes
git remote / git remote -vList remotes (with URLs)
git remote add <name> <url>Add a remote
git remote rename <old> <new>Rename it
git remote set-url <name> <url>Change its URL — e.g. after renaming the repository, or switching from HTTPS to SSH
git remote remove <name>Remove it (only the link — nothing on the server is deleted)
git remote show <name>Details about it
$
git remote rename origin github
git remote set-url github git@github.com:sam-rivera/recipes.git
git remote -v
Output
Renaming remote references: 100% (1/1), done.
github	git@github.com:sam-rivera/recipes.git (fetch)
github	git@github.com:sam-rivera/recipes.git (push)

More than one remote

A repository can have any number of remotes. Common cases:

  • Forks — origin is your fork and upstream is the original project (lesson 37).
  • Mirrors and deployment — pushing the same code to GitHub and to a hosting platform that deploys on push.
  • A colleague's copy — fetching straight from a teammate's fork to try their branch.

HTTPS and SSH URLs

FormExampleSigns in with
HTTPShttps://github.com/sam-rivera/recipes.gitA browser login or token, remembered by a credential helper
SSHgit@github.com:sam-rivera/recipes.gitAn SSH key on your computer

Both work equally well; the next lesson explains how to set either up.

Try it yourself

Create an empty repository on GitHub (or another host), add it as origin to a local project with a couple of commits, and push with git push -u origin main. Check the commits appear on the website, then run git remote show origin.