iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

git remote add

git remote: link a local repo to remote URLs. Multiple remotes, fetch / pull URLs, and the patterns for forks + mirrors.

Git — git remote

EXAMPLE
# ===== List + inspect =====
git remote                   # short names
git remote -v                 # show URLs
git remote show origin        # detailed: branches, tracking, fetch refs

# ===== Add =====
git remote add origin git@github.com:me/my-app.git
git remote add upstream git@github.com:original-owner/my-app.git
# 'upstream' is the conventional name for the fork's source.

# Then:
git fetch upstream
git merge upstream/main

# ===== Change URL =====
git remote set-url origin git@github.com:me/my-app.git
git remote set-url --push origin git@bitbucket.org:me/my-app.git   # different push URL
git remote set-url --add origin https://gitlab.com/me/my-app.git    # multiple push URLs (mirror)

# ===== Rename / remove =====
git remote rename origin github
git remote remove old-remote

# ===== Fetch / pull =====
git fetch                     # default remote = 'origin'
git fetch --all
git fetch upstream main

git pull                      # fetch + merge (or rebase per config)
git pull upstream main

# ===== Push =====
git push -u origin main       # set upstream tracking
git push                       # uses tracking branch

# Push to multiple remotes (mirror):
git remote set-url --add --push origin git@gitlab.com:me/my-app.git
git push                       # now pushes to both

# ===== HTTPS vs SSH =====
# HTTPS: https://github.com/me/my-app.git
#   - Pros: no SSH key needed; works through corporate proxies
#   - Cons: needs credential helper for repeated auth
# SSH:   git@github.com:me/my-app.git
#   - Pros: passwordless; faster on big repos
#   - Cons: requires SSH key registered

# Convert:
git remote set-url origin git@github.com:me/my-app.git    # to SSH

# ===== Tracking + relations =====
# Show what branch tracks what:
git branch -vv

# Set tracking:
git branch --set-upstream-to=origin/main main

# ===== Useful patterns =====
# Fork + sync workflow:
git remote add upstream <original-repo>
git fetch upstream
git rebase upstream/main      # keep your fork up-to-date

# Mirror to a backup remote:
git remote add mirror <backup-url>
git push mirror --mirror      # all refs, including tags + branches

# ===== Pitfalls =====
# - Remote URL pointing at old repo after a rename (use set-url)
# - HTTPS without a credential helper -> retyping every push
# - Forgetting --tags on push (tags do NOT push by default)
# - 'git pull' without knowing if it merges or rebases (configure pull.rebase)

# ===== Patterns to internalise =====
# - 'origin' for your primary remote; 'upstream' for the fork source
# - SSH for personal repos; HTTPS at work if corporate proxies require it
# - Set tracking on first push (-u)
# - 'git remote show origin' before opening a PR — sanity check

Why it matters

git remote glues a local repo to one or more remote URLs. origin for primary, upstream for fork sources. Use set-url + --add for mirrors, pair fetch with rebase for forks, and SSH where you can. The remote model is small; misconfigurations confuse most teams way more than necessary.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git remote add origin git@github.com:user/repo.git
git remote set-url origin https://github.com/user/repo.git
Try it Yourself »

Discussion

Loading…