Part III · Shell and commands
30.4 Remotes: fetch, pull and push
alsogit remoteremoteoriginupstreamfetchgit fetchpullgit pullpushgit pushrejectednon-fast-forwardforce pushforce-with-leasetracking branchdivergent branches
TL;DR
- A remote is another copy of the repository, usually
originon GitHub or GitLab. Your copy is complete: most commands work offline. fetchdownloads new commits without touching your branches;pullis fetch, then merge (or rebase) into your branch;pushsends yours.origin/mainis your last fetched view of the remote'smain, not a live one.
git remote -v
git fetch && git status # ahead or behind origin?
git branch -vv # each branch's upstream and distance
On your machine
$ atfq 30.4 Files /etc/gitconfig, ~/.gitconfig Identity name and email set Settings init.defaultbranch = main pull.rebase = true core.autocrlf = input credential.helper = store (passwords in plain text in ~/.git-credentials) Aliases st, lg Includes 1 more file, read under conditions (include, includeIf)
An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.
Pitfalls
- "Updates were rejected": the remote has commits you don't. Pull (or fetch and rebase) first, then push again.
- "Need to specify how to reconcile divergent branches": choose once,
git config --global pull.rebase true(orfalseto merge). - Prefer
--force-with-leaseto--force: it refuses if the remote moved since your last fetch, instead of erasing someone's commits. push.autoSetupRemote = truesaves the-u origin BRANCHon a new branch's first push.
Deep dive 2 questions
HTTPS or SSH remotes?
With SSH (git@github.com:user/repo.git), your SSH key authenticates
you (29.1). With HTTPS, git asks a credential helper for a
token: the system keyring, or store, which writes it in plain text in
~/.git-credentials. Hosts no longer accept account passwords over HTTPS.
What does the remote keep that I don't?
Nothing about your work: a clone has every commit and branch. What lives only on the host are pull requests, issues and permissions. That's why any clone can restore a lost server, and why a secret pushed once must be revoked: removing it from history doesn't remove it from other clones.