Lesson Completion
Back to course

git fetch

Beginner
9 minutes4.7GitPlay to LearnLearn by Story

The Hook (The "Byte-Sized" Intro)

git fetch is the polite way to check what's new. It downloads all the latest commits, branches, and tags from the remote — but it doesn't touch your working files. Nothing changes in your code. Nothing merges. You're just getting the latest information so you can review it before deciding what to do. It's the "peek before you leap" command.

📖 What is git fetch?

git fetch downloads commits, branches, and tags from a remote repository into your local repo's remote-tracking branches (e.g., origin/main). It updates your knowledge of the remote's state without modifying your working tree, staging area, or local branches.

Conceptual Clarity

  • git fetch updates remote-tracking branches (origin/main, origin/develop, etc.)
  • Your local branches are NOT affected — main stays exactly where it was
  • Your working tree is NOT modified — no files change
  • After fetching, you can inspect changes before merging them in
  • git pull = git fetch + git merge (fetch is the safe half)

What gets updated:

ComponentBefore FetchAfter Fetch
origin/mainPoints to old commitPoints to latest remote commit
Local mainUnchangedUnchanged
Working treeUnchangedUnchanged
New remote branchesNot visibleNow visible as origin/<name>

Real-Life Analogy

git fetch is like checking your mailbox. You bring all the new letters inside, but you don't open or act on them yet. You can look at the envelopes, decide which ones matter, and read them at your own pace. git pull opens and processes all the mail immediately.

Visual Architecture

flowchart LR REMOTE["☁️ Remote<br/>(origin)"] -->|"git fetch"| RTB["🔍 Remote-Tracking<br/>Branches Updated"] RTB -.->|"Not touched"| LOCAL["📂 Local Branch<br/>(unchanged)"] RTB -.->|"Not touched"| WT["💻 Working Tree<br/>(unchanged)"] style REMOTE fill:#0f3460,stroke:#53d8fb,color:#53d8fb style RTB fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style LOCAL fill:#1a1a2e,stroke:#e94560,color:#e94560 style WT fill:#1a1a2e,stroke:#e94560,color:#e94560

Why It Matters

  • Safety: See what's new without risking merge conflicts in your active work.
  • Review first: Inspect remote changes with git log or git diff before merging.
  • Awareness: Know if your branch is behind, ahead, or diverged from the remote.
  • Selective merging: Fetch once, then cherry-pick or merge only what you need.

Code

bash
# ─── Fetch from the default remote (origin) ─── git fetch # Downloads new commits, branches, and tags # ─── Fetch from a specific remote ─── git fetch upstream # ─── Fetch all remotes at once ─── git fetch --all # ─── Fetch and prune deleted remote branches ─── git fetch --prune # Removes local references to branches deleted on the remote # ─── After fetching: compare local vs remote ─── git log main..origin/main --oneline # Shows commits on remote that you don't have locally git diff main origin/main # Shows file-level differences # ─── After fetching: merge when ready ─── git merge origin/main # Now your local main is up-to-date # ─── Check ahead/behind status ─── git status # Output: Your branch is behind 'origin/main' by 3 commits

git fetch vs git pull

Aspectgit fetchgit pull
Downloads remote changes
Updates remote-tracking branches
Modifies local branch✅ (merges automatically)
Modifies working tree
Can cause merge conflicts
Lets you review first❌ (merges immediately)

Key Takeaways

  • git fetch downloads remote updates without changing your local branches or files.
  • It's the safe first step — always fetch before deciding to merge.
  • Use git log main..origin/main after fetching to see what's new.
  • git pull = git fetch + git merge. Fetch is the non-destructive half.

Interview Prep

  • Q: What is the difference between git fetch and git pull? A: git fetch downloads remote changes into remote-tracking branches but doesn't modify local branches or the working tree. git pull does a fetch AND automatically merges the remote changes into your current local branch.

  • Q: Why would you use git fetch instead of git pull? A: git fetch lets you review remote changes before integrating them. You can inspect new commits with git log, check diffs, and decide whether to merge, rebase, or cherry-pick — without risking unexpected merge conflicts in your working tree.

  • Q: What are remote-tracking branches? A: Remote-tracking branches (e.g., origin/main) are local references that mirror the state of branches on the remote. They're updated by git fetch and represent your last known snapshot of the remote's branches.

Topics Covered

Git RemotesGit Fundamentals

Tags

#git#fetch#remote#beginner-friendly

Last Updated

2026-02-12