Remote Repositories

Introduction

A remote is a named link to another copy of your repository—usually on GitHub, GitLab, or Gitee. You push local commits upstream and fetch or pull teammates’ work. This chapter adds origin, pushes main, and explains tracking branches.

Prerequisites

  • Merging Branches
  • Local repo with at least one commit
  • A blank remote repository on your hosting platform (no README if pushing existing history—or follow platform hints)

View and Add Remotes

List configured remotes:

bash
git remote -v

Empty on a fresh git init repo.

Add remote named origin (convention):

bash
git remote add origin git@github.com:YOUR_USER/my-app.git

Or HTTPS:

bash
git remote add origin https://github.com/YOUR_USER/my-app.git

Verify:

bash
git remote -v
text
origin  git@github.com:YOUR_USER/my-app.git (fetch)
origin  git@github.com:YOUR_USER/my-app.git (push)

Code explanation:

  • origin is default name for primary remote—not required but universal
  • Same URL used for fetch and push unless configured separately

First Push with Upstream

bash
git push -u origin main

Code explanation:

  • push sends local commits to remote branch
  • -u sets upstream tracking so later you can git push / git pull without repeating branch name

Success creates main on remote and links mainorigin/main.

fetch vs pull

git fetch

Download remote commits without merging into your branch:

bash
git fetch origin

Inspect:

bash
git log --oneline main..origin/main
git branch -a

Code explanation:

  • Safe preview before integrating
  • Updates remote-tracking branches like origin/main

git pull

Fetch and merge (default) into current branch:

bash
git pull origin main

Shorthand when upstream set:

bash
git pull

Code explanation:

  • pull = fetch + merge (by default)
  • Can reconfigure to rebase with pull.rebase true—advanced

Tracking Branch (Upstream)

After -u:

bash
git status

May show:

text
Your branch is up to date with 'origin/main'.

Behind remote:

text
Your branch is behind 'origin/main' by 2 commits.

Run git pull to catch up.

Set upstream on existing branch:

bash
git push -u origin feature/login

Clone (Remote → Local)

Opposite of push—start from remote:

bash
git clone git@github.com:YOUR_USER/my-app.git
cd my-app

Code explanation:

  • Creates folder, full history, and origin remote automatically
  • Default checkout is platform default branch (main)

See Repositories and the Working Tree.

Rename or Change Remote URL

bash
git remote rename origin upstream
git remote set-url origin https://github.com/NEW_USER/repo.git

Remove remote:

bash
git remote remove origin

Typical Daily Flow

bash
git fetch origin
git switch main
git pull
git switch -c feature/api
# ... work, commit ...
git push -u origin feature/api

Bare vs Non-Bare (Concept)

TypeHas working tree?Use
Non-bareYesYour laptop, normal clone
BareNo—only .git dataCentral server receiving pushes

Hosting platforms store bare repos; you never commit directly on server disk.

FAQ

push rejected (non-fast-forward)?

Remote has commits you lack—git pull first, resolve conflicts, push again.

No upstream branch?

First push needs -u origin branch-name.

fetch shows nothing new?

Already up to date—or wrong remote URL.

Multiple remotes?

origin for your fork, upstream for original project—chapter 26.

HTTPS vs SSH?

Chapter 13—both work; SSH avoids repeated password prompts.

Delete remote branch?

git push origin --delete feature/old—coordinate with team.