devutils/ATFQ/book ATFQ git

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 origin on GitHub or GitLab. Your copy is complete: most commands work offline.
  • fetch downloads new commits without touching your branches; pull is fetch, then merge (or rebase) into your branch; push sends yours.
  • origin/main is your last fetched view of the remote's main, 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

alice@laptopexample
$ 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

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.

Commands
git
See also
30.3 Merge or rebase?
29.1 SSH keys and the agent