Part V: Chapter 5.3

git fetch

Updating remote-tracking branches without touching your work.

git fetch contacts the remote, downloads any new objects, and advances the remote-tracking branches to reflect the current state of the remote. It never touches your local branches, your index, or your working directory. It is always safe to run.

Fetch in Action

The remote has two new commits. Hit fetch and watch what updates — and what stays completely untouched.

origin (GitHub)
main
g8h9
9f8aInitial commit
a1b2Add homepage
c4d5Add styles
e6f7New landing pagenew
g8h9Hotfix: nav crashnew
Local repository
main (HEAD)
c4d5 — never moves on fetch
9f8aInitial commit
a1b2Add homepage
c4d5Add styles
origin/main
c4d5 (stale)
Your main is at c4d5. The remote has 2 more commits you don't have yet. origin/main is stale — it hasn't been updated since your last communication with the remote.

fetch vs pull

git pull is simply git fetch followed by git merge (or git rebase if configured). It updates the remote-tracking branch and immediately integrates the changes into your local branch.

Many developers prefer to fetch first so they can inspect what changed — git log origin/main, git diff main origin/main — and then consciously decide when and how to integrate.

# These two are equivalent:
git pull origin main
git fetch origin
git merge origin/main

Why Fetch is Safe

Because fetch only updates refs and downloads objects — it never modifies the working directory or moves any local branch pointer — it cannot cause conflicts, data loss, or surprise state changes.

This makes it ideal for:

  • Running on CI before builds to get the latest remote state
  • Checking if colleagues have pushed before starting work
  • Getting new branches from the remote without switching to them
  • Running in the background periodically to keep tracking refs fresh

After a Fetch — Inspecting the Difference

The most useful thing to do after fetching is understand what you got before integrating it.

Terminal
$git fetch origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
From https://github.com/badu/myrepo
c4d5e6f..g8h9i0j main -> origin/main
$# See the new commits:
$git log main..origin/main --oneline
g8h9i0j Hotfix: nav crash
e6f7g8h New landing page
$# See what files changed:
$git diff main origin/main --stat
index.html | 12 +++++++-----
nav.js | 3 +--
$# Integrate when ready:
$git merge origin/main
Updating c4d5e6f..g8h9i0j
Fast-forward