Skip to content

Lesson 9 of 14 · 18m

origin/main Lives on Your Disk: fetch, Tracking, and pull

Remote-tracking refs, the refspec, upstream config, and why pull is fetch plus a second step.

git fetch doesn't touch your files. It downloads objects and updates your local copies of the remote's branches — the refs under refs/remotes/origin/* in your own .git. origin/main isn't the remote's main branch. It's your repository's memory of the remote's main, accurate as of your last fetch. Once that lands, the daily commands stop looking magical: git status saying "behind 'origin/main' by 4" is comparing two pointers on your disk, and git pull is just fetch plus a second step.

The refspec

Open .git/config and you'll find the wiring:

[remote "origin"]
    url = git@github.com:corp/app.git
    fetch = +refs/heads/*:refs/remotes/origin/*

That last line is the refspec — the map that says "the remote's refs/heads/* becomes my refs/remotes/origin/*." Fetch is a write to that namespace, not to your branches. git ls-remote origin shows what the remote actually holds right now without writing anything — the read-only probe for "did main move?"

One-off fetches: FETCH_HEAD

Fetch outside the configured refspec writes no tracking ref at all — git fetch origin release/2.4 drops the fetched tip into .git/FETCH_HEAD, which then works like any revision: git log HEAD..FETCH_HEAD, git merge FETCH_HEAD. It's the tool for peeking at a branch you don't want remembered under origin/*. GitHub leans on the same mechanics to expose pull requests as refs — git fetch origin pull/412/head pulls PR #412 into FETCH_HEAD straight from the upstream repo, no fork clone required, which is the standard way to build a PR locally for review.

fetch vs pull, precisely

git pull = git fetch + one more command, decided by config: git merge (default), git rebase (pull.rebase=true), or nothing (pull.ff=only aborts unless a fast-forward is possible). Those mystery commits titled "Merge branch 'main' of github.com:corp/app" are pull-with-merge on a diverged local branch — bubbles, not work.

The deliberate workflow looks like this:

git fetch origin
git log HEAD..origin/main        # what's coming in
git diff HEAD...origin/main      # the change, three-dot (merge base to their tip)
git merge origin/main            # or rebase — your choice, on purpose

git checkout origin/main "works" but lands you on detached HEAD — git switch main knows about the tracking ref and stays attached.

Upstream tracking

git branch -vv shows the mapping: feature [origin/feature: ahead 2, behind 1]. The link lives in .git/config as branch.feature.remote/branch.feature.merge. It's what plain git pull and git push consult, and it's why a fresh branch pushed without -u makes git pull fail with "no tracking information" — git doesn't know which remote branch maps here.

git push -u origin feature          # sets it on first push
git branch --set-upstream-to=origin/feature

Since git 2.37, git config --global push.autoSetupRemote true makes this automatic on first push — set it once and the -u forgetfulness problem disappears.

Stale state

Fetch is also the only thing that refreshes your picture, and parts of it deliberately stay stale. Branches deleted on GitHub keep their origin/* refs locally — git fetch --prune removes refs whose remote branch is gone (git config fetch.prune true makes it routine). git push updates your origin/* ref for the branch you pushed and nothing else.

The error everyone meets is ! [rejected] main -> main (non-fast-forward): the remote has commits your origin/main — and you — don't. The fix is fetch, look, then merge or rebase. Never --force past it blind; --force-with-lease (next lesson) is the version that checks you're only overwriting what you already saw.

Forks are just a second remote: git remote add upstream git@github.com:corp/app.git, then git fetch upstream + git rebase upstream/main keeps your fork current. git remote update is a fetch across every remote at once.

Pitfalls

  • git pull on the wrong branch merges upstream into it. Always know which branch you're standing on before pulling — the refspec doesn't care, and now you're recovering.
  • "behind" counts lie while stale. Without a recent fetch, status compares against last week's memory. Fetch before you interpret ahead/behind.
  • Remote-tracking refs are read-only by convention. Committing on origin/main detaches HEAD; treat origin/* as reports, not branches.

Try this

git ls-remote origin | head, then git branch -r — compare the remote's claim to your memory. git fetch, run the comparison again, then git log HEAD..origin/main to read what came in before deciding to take it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.