Skip to content

git fetch

Downloading remote changes with git fetch — the safe alternative to git pull that does not merge.

What you'll learn

  • How git fetch downloads changes without modifying your working directory
  • Why fetch is safer than pull
  • Fetching specific branches and remotes
  • Inspecting fetched changes before merging

Concept

What git fetch Does

git fetch downloads commits, files, and refs from a remote repository, but does not merge them into your local branches. Your working directory stays unchanged. This makes it the safe way to see what is new on the remote without risking your local work.

git fetch

After fetching, the remote-tracking branches (like origin/main) are updated, but your local main is not. You can inspect the changes and decide when (or whether) to merge.

fetch vs pull

This is a crucial distinction:

  • git fetch — downloads remote changes. Your local branches and working directory are NOT modified. Safe.
  • git pull — runs git fetch THEN git merge. Your local branch IS modified. Can create merge commits or conflicts.
git pull   =   git fetch   +   git merge

git pull is convenient but can surprise you with merge commits or conflicts. git fetch gives you control — you see what changed first, then decide how to integrate it.

The Safe Update Workflow

# 1. Fetch remote changes (does not touch your working directory)
git fetch origin

# 2. See what changed on the remote
git log --oneline main..origin/main
# shows commits on origin/main that are not in your local main

# 3. See the actual diff
git diff main origin/main

# 4. When you are ready, merge
git merge origin/main
# or rebase:
git rebase origin/main

This workflow lets you review changes before they touch your code.

Fetching Specific Branches

git fetch origin main           # fetch only the main branch
git fetch origin feature-login  # fetch a specific feature branch
git fetch upstream              # fetch from the upstream remote
git fetch --all                 # fetch from all remotes

Fetching and Pruning

When branches are deleted on the remote, your local remote-tracking references become stale. Prune them:

git fetch --prune
# or set it to happen automatically:
git config --global fetch.prune true

This removes references to remote branches that no longer exist, keeping your branch list clean.

Shallow Fetch

If you did a shallow clone, you can fetch more history:

git fetch --unshallow       # fetch the complete history
git fetch --depth=100       # fetch up to 100 commits deep

When to Use fetch vs pull

Use git fetch when:

  • You want to see what is new before integrating it
  • You are not ready to merge (you have local work in progress)
  • You want to avoid unexpected merge commits
  • You are reviewing a colleague's branch

Use git pull when:

  • You just want to get the latest and continue working
  • You are on a feature branch and want to stay in sync
  • You are confident there will be no conflicts

Many experienced developers prefer git fetch + git merge over git pull for the control it provides.

Fetching Tags

git fetch --tags          # fetch all tags
git fetch --all --tags    # fetch all remotes and all tags

Tags are not fetched by default (since Git 2.0), so you need --tags if you want them.

Example

              # --- The safe update workflow ---
git fetch origin

# See what commits are new on the remote
git log --oneline main..origin/main
# abc1234 feat: add search feature (by teammate)
# def5678 fix: resolve pagination bug (by teammate)

# See the actual changes
git diff main origin/main --stat
# src/search.js    | 45 +++++++++++++++++
# src/pagination.js | 12 +++--

# Now decide: merge or rebase?
git merge origin/main
# or:
git rebase origin/main

# --- Fetch a specific branch to review ---
git fetch origin feature-login
git log --oneline origin/feature-login -5
git diff main origin/feature-login --stat

# --- Fetch from upstream (forking workflow) ---
git fetch upstream
git log --oneline main..upstream/main
git merge upstream/main

# --- Prune deleted remote branches ---
git fetch --prune
# - [deleted]     (none) -> origin/old-feature

# --- Set automatic pruning ---
git config --global fetch.prune true
git fetch  # prunes automatically from now on

# --- Fetch all remotes ---
git fetch --all

# --- Compare fetch vs pull ---
git fetch origin          # safe: nothing changes locally
git log origin/main       # see what the remote has
# ... decide to merge ...
git merge origin/main     # now integrate the changes
            

Shows the safe fetch-then-review-then-merge workflow, fetching specific branches for review, fetching from upstream, pruning deleted branches, and the comparison between fetch and pull. The key habit is: fetch first, review the diff, then merge when ready.

Try it

Common mistakes

The mistake

Using git pull and being surprised by an unexpected merge commit

The fix

git pull = git fetch + git merge. If the remote and local have diverged, pull creates a merge commit. To avoid this, use git fetch first to see what is new, then git rebase origin/main to keep your history linear. Or use git pull --rebase to do it in one step.

The mistake

Forgetting to prune and seeing stale remote branches in git branch -r

The fix

When branches are deleted on the remote, local references persist until pruned. Run git fetch --prune to clean them up, or set git config --global fetch.prune true to prune automatically on every fetch. This keeps your branch list accurate.

Related Snippets

Related Cheatsheets

Practice git fetch

3 exercises

Practice git fetch

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors