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 purposegit 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/featureSince 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 pullon 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,
statuscompares against last week's memory. Fetch before you interpret ahead/behind. - Remote-tracking refs are read-only by convention. Committing on
origin/maindetaches HEAD; treatorigin/*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?
Keep reading
Related articles from the SudoSecurity blog.
What is Git? The Version Control System Every Developer Uses
Git tracks every change to every file in your codebase, lets multiple developers work simultaneously without overwriting each other, and gives you a complete history you can rewind to any point. It is the foundation of modern software development. Here is how it actually works.
VSCode 1.72: Git Source Control has some good enhancements.
The latest version of Microsoft’s Visual Studio Code code editor focuses on improvements to the toolbar and autoscrolling, as well as Git repos. Unveiled October 6, Visual Studio Code 1.72 is also known as the September 2022 release, as it features fixes and features from last month. Toolbar
Apple's WebKit comes to GitHub
The development of WebKit, the open source web browser engine at the heart of Apple’s Safari web browser, has migrated over to GitHub. The WebKit project team announced August 31 that the project had frozen its Subversion tree on June 23 and transitioned management and interaction with the source
GitHub Open Sources Entitlements IAM
GitHub is making available a new IAM (Identity and Access Management) tool, dubbed Entitlements, which leverages the company's own Git framework to parse, track and approve access to a business' systems. The basic idea of Entitlements is to use a dedicated Git repository as a way to provide a…