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

Git fetch Command

git fetch is like checking the mailbox and bringing in the new letters without opening them or changing your desk. You see what is new but nothing of yours changes yet.
Syntax
bash
git fetch remote_name
git fetch remote_name branch_name

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting git fetch to change your files, when it only updates remote-tracking branches and leaves your working directory unchanged.
  2. Running git fetch and thinking you are up to date, when you still need git merge origin/main to bring the commits in.
  3. Fetching a branch name that does not exist on the remote, like git fetch origin featur, which gives a couldn't find remote ref error.

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.