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:
git remote -vEmpty on a fresh git init repo.
Add remote named origin (convention):
git remote add origin git@github.com:YOUR_USER/my-app.gitOr HTTPS:
git remote add origin https://github.com/YOUR_USER/my-app.gitVerify:
git remote -vorigin git@github.com:YOUR_USER/my-app.git (fetch)
origin git@github.com:YOUR_USER/my-app.git (push)Code explanation:
originis default name for primary remote—not required but universal- Same URL used for fetch and push unless configured separately
First Push with Upstream
git push -u origin mainCode explanation:
pushsends local commits to remote branch-usets upstream tracking so later you cangit push/git pullwithout repeating branch name
Success creates main on remote and links main → origin/main.
fetch vs pull
git fetch
Download remote commits without merging into your branch:
git fetch originInspect:
git log --oneline main..origin/main
git branch -aCode explanation:
- Safe preview before integrating
- Updates remote-tracking branches like
origin/main
git pull
Fetch and merge (default) into current branch:
git pull origin mainShorthand when upstream set:
git pullCode explanation:
pull=fetch+merge(by default)- Can reconfigure to rebase with
pull.rebase true—advanced
Tracking Branch (Upstream)
After -u:
git statusMay show:
Your branch is up to date with 'origin/main'.Behind remote:
Your branch is behind 'origin/main' by 2 commits.Run git pull to catch up.
Set upstream on existing branch:
git push -u origin feature/loginClone (Remote → Local)
Opposite of push—start from remote:
git clone git@github.com:YOUR_USER/my-app.git
cd my-appCode explanation:
- Creates folder, full history, and
originremote automatically - Default checkout is platform default branch (
main)
See Repositories and the Working Tree.
Rename or Change Remote URL
git remote rename origin upstream
git remote set-url origin https://github.com/NEW_USER/repo.gitRemove remote:
git remote remove originTypical Daily Flow
git fetch origin
git switch main
git pull
git switch -c feature/api
# ... work, commit ...
git push -u origin feature/apiBare vs Non-Bare (Concept)
| Type | Has working tree? | Use |
|---|---|---|
| Non-bare | Yes | Your laptop, normal clone |
| Bare | No—only .git data | Central 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.