Remotes explained
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:
git remote -vorigin 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:
[remote "origin"]
url = git@github.com:asha/shop.git
fetch = +refs/heads/*:refs/remotes/origin/*Remote-tracking branches
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>:
git branch -a* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/feature/searchmainis your branch. You commit to it.origin/mainis your record of GitHub’smain. 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:
git branch -vv* main a1b2c3d [origin/main: ahead 1] Add pricing page
feature/search 4e5f6a7 [origin/feature/search] Add search boxUpstreams 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
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:
mainis yours;origin/mainis your last postcard from GitHub. Fetch to get a new postcard.
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.
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 remotesgit 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
git remote add origin ../server.git
git push -u origin main
# 2. Inspect the remote
git remote -v
git remote show origin
# 3. Compare main and origin/main
git branch -vv # * main abc1234 [origin/main] Initial commit
[ "$(git rev-parse main)" = "$(git rev-parse origin/main)" ] && echo "in sync"
# 4. Add a second remote, backup, and list all remotes
git remote add backup ../backup.git
git remote # backup, originSummary
- A remote is a named URL of another copy of the repository — usually on GitHub.
originis just the conventional name for the remote you cloned from or push to by default.origin/mainis a remote-tracking branch: your local record of wheremainwas onoriginlast 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,renameandremove.