push, pull & fetch

Syncing with the Remote

Once a remote is linked, three commands move commits between your machine and it:

CommandDirectionDoes
git pushlocal → remoteUpload your local commits so the remote has them too
git fetchremote → localDownload commits from the remote, but don't touch your working files or branch
git pullremote → localFetch and then immediately merge the new commits into your current branch
$ git push origin main
$ git pull            # = git fetch + git merge
Analogy: fetch is checking the mailbox - you see what arrived without doing anything with it yet. pull is checking the mailbox and immediately filing everything into your drawers (fetch + merge in one step). push is dropping your own outgoing letters in the mailbox for others to receive.

After a git fetch, Git updates a special kind of pointer called a remote-tracking branch - shown as origin/main - which represents where main is on the remote as of your last fetch. It's a read-only bookmark; you compare against it (git log main..origin/main to see what you're missing) or merge it (which is exactly what git pull automates for you).

Warning: git pull can trigger a merge conflict if the remote changed lines you also changed locally. Commit or stash your work before pulling, so you're never merging on top of a messy, half-finished working tree - that combination makes conflicts much harder to reason about.

First push of a brand-new branch needs to set up the link (the "upstream") between your local branch and its remote counterpart, since none exists yet:

$ git push -u origin feature-login   # -u links local ↔ remote branch
$ git push                           # afterwards, Git remembers - just this