Remotes Overview
A remote is a named URL for another git repo — usually origin on GitHub/GitLab. git fetch / pull / push all talk to remotes. You can have many.
Add, fetch, push, multiple remotes
EXAMPLE
# 1) Inspect existing remotes git remote git remote -v # with URLs # origin git@github.com:me/myapp.git (fetch) # origin git@github.com:me/myapp.git (push) # 2) Add a remote git remote add origin git@github.com:me/myapp.git git remote add upstream https://github.com/upstream/repo.git # 3) Change the URL git remote set-url origin git@github.com:me/myapp-renamed.git git remote set-url --push origin git@github.com:me/myapp.git # different fetch/push URLs # 4) Rename / remove git remote rename origin upstream git remote remove old-fork # 5) Fetch — bring down refs without merging git fetch origin # all branches + tags git fetch origin main # one branch git fetch --all # every remote git fetch --prune # delete local refs for branches deleted upstream # 6) Pull — fetch + merge (or rebase) git pull # current branch's tracked upstream git pull origin main git pull --rebase # rebase your work on top instead of merging # Set rebase by default (cleaner history): git config --global pull.rebase true # 7) Push git push # current branch to its upstream git push origin main git push origin HEAD:feature/new # push current HEAD to a remote branch git push -u origin feature/new # set tracking on first push git push --tags # tags git push origin --delete feature/done # delete a remote branch # Force push — DANGEROUS, use --force-with-lease git push --force-with-lease # refuses if remote moved git push --force # blind overwrite — only on personal branches # 8) Tracking branches — local branch knows its remote counterpart git branch -vv # see tracking + last commit git branch -u origin/main main git branch --set-upstream-to=origin/feature/x feature/x # 9) Multiple remotes — fork workflow # You forked someone's repo: git remote add origin git@github.com:you/repo.git # your fork git remote add upstream git@github.com:original/repo.git # the source git fetch upstream git rebase upstream/main # sync your branch with upstream git push # push to your fork # 10) Different remotes for fetch vs push # Read from a fast mirror, write to the canonical repo git remote set-url --push origin git@github.com:me/repo.git git remote set-url origin https://github.com/me/repo.git # 11) Show a remote's details git remote show origin # * remote origin # Fetch URL: … # Push URL: … # HEAD branch: main # Remote branches: # main tracked # feature/auth tracked # Local branches configured for 'git pull': # main merges with remote main # Local refs configured for 'git push': # main pushes to main (up to date) # 12) Prune stale refs (branches deleted on the remote) git fetch --prune git remote prune origin # Auto-prune on fetch: git config --global fetch.prune true # 13) Mirror push — copy ALL refs (use for migrations) git clone --mirror git@github.com:old/repo.git cd repo.git git remote set-url --push origin git@gitlab.com:new/repo.git git push --mirror # 14) Authentication # SSH (recommended): ssh-keygen + add to GitHub/GitLab → git@host:user/repo.git # HTTPS + token: git@... but using a Personal Access Token as password # Credential helper: git config --global credential.helper osxkeychain | manager | cache --timeout=3600 # 15) Common pitfalls # • Pushing to the wrong remote (origin vs upstream) — re-check with 'git remote -v' before each push # • Forgetting --prune — local has stale tracking refs after teammate deletes a branch # • Pull without --rebase on a busy team — messy merge commits # • --force on shared branches — overwrites teammate's work # • Hardcoded production credentials in remote URL — use SSH or credential helper # 16) Audit what you'd push (before pushing) git log origin/main..HEAD # commits you have but origin doesn't git diff origin/main..HEAD # diff of those commits
Why it matters
git fetch --prune + --force-with-lease are the two safety habits that prevent 90% of remote-related accidents. Run prune weekly, never use plain --force on a shared branch.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…