Tour of Git
Working with Remotes / Lesson 4.6

git fetch — Downloading Without Merging

The git fetch command downloads commits, branches, and tags from a remote repository — but unlike git pull, it does not modify your working directory or current branch. This makes it the safe way to see what others have been doing.

How Fetch Works

When you run git fetch, Git contacts the remote and downloads any new commits. These commits are stored in your remote tracking branches (like origin/main), leaving your local branches untouched.

Before fetch:
  local:   A --- B --- C  (main)
  remote:  A --- B --- D --- E  (origin/main on server)

After fetch:
  local:   A --- B --- C  (main)      ← unchanged!
  remote tracking:  A --- B --- D --- E  (origin/main)

Your main still points to C. The new commits D and E are available in origin/main for you to inspect.

Basic Usage

git fetch origin

This fetches all branches from the origin remote. You can also fetch a specific branch:

git fetch origin main

Inspecting Fetched Changes

After fetching, you can review the changes before deciding to merge:

git log main..origin/main --oneline

This shows commits that exist in origin/main but not in your local main. You can also see what changed:

git diff main origin/main

Fetch vs Pull

CommandDownloads?Merges?Safe?
git fetchYesNoVery safe
git pullYesYesCan cause conflicts

Think of git pull as git fetch + git merge combined. When in doubt, fetch first.

Try it!

Practice fetching and inspecting remote changes before merging:

  1. Check your current log: git log --oneline
  2. Fetch updates from origin: git fetch origin
  3. See what new commits arrived: git log main..origin/main --oneline
  4. Compare the differences: git diff main origin/main
  5. When you're satisfied, merge manually: git merge origin/main
  6. Confirm the merge: git log --oneline

When to Use Fetch

  • Before starting work — Fetch to see if there are updates, then decide how to integrate them
  • When you want to review — Fetch lets you inspect changes before merging
  • In scripts — Fetch is predictable because it never modifies your working tree
  • When you're in the middle of something — Fetch won't disrupt your current work
🎯

Goal

Fetch changes from origin and then merge them into your local branch

Terminal
$

No commits yet. Run `git init` and create your first commit.