Git Course 0%

git fetch

Intermediate Git CLI Safe ≈ 5 min

Summary: git fetch downloads commits, branches and tags from the remote into your repository and updates the remote-tracking bookmarks such as origin/main. It changes no branch of yours and no file in your working tree.

Safe — the only network command that cannot disturb anything. Run it at any moment, including mid-edit.

What it does

Contacts the remote, downloads any objects you lack, and moves your origin/… refs to match the server. Your own branches stay exactly where they were, which is what makes the operation safe and what makes git status newly accurate afterwards.

Why it exists

So that "what has the team done?" and "do I want it?" are separate questions. git pull answers both at once, which is convenient and occasionally the wrong moment.

When to use it

  • Before trusting git status's ahead/behind line, which compares against the bookmark (lesson 7.5).
  • To see what is coming without accepting it: fetch, then git log --oneline main..origin/main.
  • While you have uncommitted work, when git pull would be refused.
  • To pick up new branches and tags colleagues have pushed.

When not to use it

  • When you actually want the changes in your files: git pull.
  • It is never harmful, so there is no situation to avoid; the only cost is a moment of network.

Syntax

Form Meaning
git fetch From the current branch's remote, usually origin
git fetch origin From a named remote
git fetch --all From every configured remote
git fetch --prune Also delete bookmarks for branches removed on the remote
git fetch --tags Also fetch tags not reachable from fetched branches
git fetch origin <branch> Just one branch

Examples

Terminal
$ git fetch
From https://gitlab.com/northwind-trails/trailguide
   6050e19..f75f2fe  main       -> origin/main

The last line reads: the remote's main moved from 6050e19 to f75f2fe, and your bookmark now says so. Your branch has not moved:

Terminal
$ git status -sb
## main...origin/main [behind 1]

Read what arrived before accepting it:

Terminal
$ git log --oneline main..origin/main
f75f2fe docs: link FAQ from README

Pruning bookmarks for branches deleted on the server:

Terminal
$ git fetch --prune
From https://gitlab.com/northwind-trails/trailguide
 - [deleted]         (none)     -> origin/docs/40-temp

Silence means nothing new.

Expected result

New objects in .git, updated origin/… refs, and nothing else. No file changes, no branch movement, no possibility of a conflict.

Common mistakes

  • Expecting your files to change. That is git pull; fetch deliberately does not.
  • Trusting git status without fetching. "Up to date" means up to date with the last fetch (lesson 7.5).
  • Stale bookmarks accumulating. Set fetch.prune true once (lesson 3.5).
  • git fetch then git merge by hand when git pull would have done both. Harmless, just longer.

How to undo or recover

Nothing to undo: fetch only adds objects and moves bookmarks that describe the server. If a bookmark moved somewhere surprising, the server moved.

In VS Code

… menu → Fetch, or Git: Fetch / Git: Fetch (Prune) in the Command Palette. Turning on Git: Autofetch in Settings keeps the ahead/behind counts in the status bar current automatically.

In IntelliJ IDEA

Git → Fetch. The branches popup then shows incoming counts, and the Log tab shows origin/main ahead of main.

git pull is fetch plus merge · git status interprets what fetch downloaded · git log with main..origin/main lists it · git remote configures where fetch goes.

Lessons that use this command

fetch vs pull; origin/main vs main · clone, remote, fetch, pull, push · Remote branches and cleanup · Lab 2.

Key terms

Fetch Remote-tracking branch origin Remote