Git Remotes
Purpose
Remotes are named references to other copies of the repository (typically on GitHub, GitLab, or a private server). They enable collaboration by providing a shared point of truth to push to and pull from.
Architecture
A remote is stored as a URL alias in .git/config. The conventional name for the primary remote is origin.
Remote-tracking branches (e.g. origin/main) are local read-only refs that record the last-known state of the remote. They are updated by git fetch.
Local repo: Remote (origin):
main main
origin/main ←─── main (remote)
After a git fetch, origin/main advances but your local main does not — you decide when to merge or rebase.
Implementation Notes
Manage remotes:
git remote -v # list remotes with URLs
git remote add <name> <url> # add a remote
git remote remove <name> # remove a remote
git remote rename <old> <new> # rename
git remote set-url <name> <url> # change URLFetch (download objects + update remote-tracking branches; does not touch local branches):
git fetch # fetch from origin
git fetch <remote> # fetch from specific remote
git fetch --all # fetch all remotesPush (upload commits to remote):
git push # push current branch to its upstream
git push <remote> <branch> # explicit
git push -u origin <branch> # push and set upstream tracking
git push --force-with-lease # force-push safely (fails if remote has new commits)
git push origin --delete <branch> # delete a remote branchPull = git fetch + git merge (or rebase if configured):
git pull # pull current branch from its upstream
git pull --rebase # pull with rebase instead of mergePrefer git fetch + explicit git merge/git rebase over bare git pull for more control and visibility.
Trade-offs
git pull(merge) creates merge commits on every pull;git pull --rebasekeeps a linear history.git push --forceoverwrites remote history — dangerous on shared branches. Always prefer--force-with-lease.- For HTTPS remotes, credentials can be cached with the system keychain or a credential helper. For SSH remotes, set up an SSH key pair for passwordless auth.