← Back to Git Course | Chapter 4: Remote Repositories | Lesson 4 of 11

Git fetch Command

What is Git Fetch?

git fetch downloads new commits, branches, and tags from a remote repository into your local database -- but critically, it never touches your working files or current branch, making it a completely safe way to check what's changed.

Example: What is Git Fetch?

bash
git fetch origin

Fetch vs Pull

The key difference from git pull is that fetch stops after downloading: it updates your remote-tracking branches (like origin/main) but leaves your actual local branches untouched, letting you review the incoming changes with git diff or git log before merging anything.

Example: Fetch vs Pull

bash
git fetch origin
git log origin/main

Fetching Specific Branches

git fetch origin <branch> targets just one specific branch instead of every branch on the remote, which saves time and bandwidth on large repositories where you only care about updates to one line of work.

Example: Fetching Specific Branches

bash
git fetch origin feature-x

Pruning Obsolete Branches

When teammates delete branches on the remote, your local remote-tracking references to them can become stale; git fetch --prune cleans those up automatically, deleting local references to branches that no longer exist on the remote.

Example: Pruning Obsolete Branches

bash
git fetch --prune

Dry Run Fetching

git fetch --dry-run shows exactly what a real fetch would download without actually downloading it -- a lightweight way to test your connection or preview incoming changes before committing to the actual transfer.

Example: Dry Run Fetching

bash
git fetch --dry-run

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.