Remotes explained

Topic 1 of 4 in Working with remotes6 min readBeginnerFree course

Overview

So far, everything has happened in one repository on your computer. Collaboration needs other copies — a shared one on GitHub, your colleagues’ clones, maybe a backup. Git calls those other repositories remotes. This lesson explains how your repository keeps track of them; the next lessons move commits back and forth.

What a remote is

A remote is simply a name for the URL of another repository:

Terminal
git remote -v
Text
origin	git@github.com:asha/shop.git (fetch)
origin	git@github.com:asha/shop.git (push)

There’s nothing magic about the name origin. It’s the name git clone gives to the repository you cloned from, and the convention for “the main shared copy”. You’ll often see a second remote called upstream when you contribute to someone else’s project (Forks and contributing).

Remotes are stored in .git/config:

INI
[remote "origin"]
	url = git@github.com:asha/shop.git
	fetch = +refs/heads/*:refs/remotes/origin/*

Remote-tracking branches

Your repository and its remote. On your computer: commits a1, 7d, c9 and e4, with main at e4 and origin/main at c9 — origin/main is where main was on GitHub the last time you fetched. On origin (GitHub, github.com/asha/shop.git): commits a1, 7d and c9 with main at c9. push sends commits from your computer to origin; fetch brings them back. git remote -v shows origin git@github.com:asha/shop.git (fetch).

Your repository can’t see the remote live. Instead, it keeps a local record of where each branch on the remote was the last time you communicated with it. These records are remote-tracking branches, named <remote>/<branch>:

Terminal
git branch -a
Text
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/feature/search
  • main is your branch. You commit to it.
  • origin/main is your record of GitHub’s main. You never commit to it; Git updates it when you fetch, pull or push.

In the diagram, you’ve made commit e4 locally, so main is ahead of origin/main. GitHub doesn’t have e4 until you push.

Remote-tracking branches are snapshots of the past: if a teammate pushed five minutes ago, origin/main doesn’t know until you run git fetch. That’s why git status says “Your branch is up to date with ‘origin/main’” — it means up to date with your last fetch, not necessarily with GitHub right now.

Upstream (tracking) branches

A local branch can be linked to a remote-tracking branch — its upstream. Then Git knows where git pull and git push should go by default, and git status can compare them:

Terminal
git branch -vv
Text
* main           a1b2c3d [origin/main: ahead 1] Add pricing page
  feature/search 4e5f6a7 [origin/feature/search] Add search box

Upstreams are set automatically when you clone, when you push with -u, or by push.autoSetupRemote (from Installing and configuring Git). You can set one by hand: git branch -u origin/main.

Managing remotes

Terminal
git remote add origin git@github.com:asha/shop.git   # add a remote
git remote -v                                         # list remotes with URLs
git remote show origin                                # details: branches, tracking, push/pull config
git remote set-url origin https://github.com/asha/shop.git   # change the URL (e.g. HTTPS ↔ SSH)
git remote rename origin github                       # rename
git remote remove backup                              # remove (deletes its remote-tracking branches too)

Removing a remote only removes the name and the tracking records in your repository. Nothing on the remote server is affected.

Remote URLs

A remote URL can be:

Form Example Notes
HTTPS https://github.com/asha/shop.git works through most firewalls; authenticate with a token or credential helper
SSH git@github.com:asha/shop.git authenticate with an SSH key
local path ../server.git or /srv/git/shop.git another repository on the same machine — great for practice

Authentication with SSH and tokens explains how to choose between HTTPS and SSH.

Bare repositories

A bare repository has no working directory — just the contents of .git. That’s what servers like GitHub store: nobody edits files there, they only push and fetch. You create one with git init --bare, and by convention its folder name ends in .git.

The exercises in this chapter use local bare repositories as a stand-in for GitHub, so you can practise pushing and pulling offline. Every command works identically with a real GitHub URL.

Mental model: main is yours; origin/main is your last postcard from GitHub. Fetch to get a new postcard.

Exercise

Connect a local project to a “server”

To practise without an internet connection, you’ll use a bare repository on your own disk as a stand-in for GitHub. Create a bare repository server.git, then in an existing local project add it as the remote origin, push main to it, and inspect the remote with git remote -v and git remote show origin. Confirm that a remote-tracking branch origin/main now exists and points at the same commit as main. Finally, add a second remote called backup (another bare repository) and list both.

Terminal
git init -q --bare -b main server.git           # pretend this is GitHub
git init -q --bare -b main backup.git

mkdir project && cd project && git init -qb main
echo "# Project" > README.md && git add . && git commit -qm "Initial commit"

# 1. Add server.git as origin and push main

# 2. Inspect the remote

# 3. Compare main and origin/main

# 4. Add a second remote, backup, and list all remotes

Summary

  • A remote is a named URL of another copy of the repository — usually on GitHub.
  • origin is just the conventional name for the remote you cloned from or push to by default.
  • origin/main is a remote-tracking branch: your local record of where main was on origin last time you talked to it.
  • Remote-tracking branches only update when you fetch, pull or push — they’re not live.
  • Manage remotes with git remote add, -v, show, set-url, rename and remove.