0
0
GitHow-ToBeginner · 3 min read

How to Use Git Fetch: Basic Syntax and Examples

Use git fetch to download updates from a remote repository without changing your local files. It updates your remote-tracking branches so you can review changes before merging.
📐

Syntax

The basic syntax of git fetch is simple and allows you to specify the remote and branch you want to fetch from.

  • git fetch [remote]: Fetches all branches from the specified remote.
  • git fetch [remote] [branch]: Fetches a specific branch from the remote.
  • git fetch --all: Fetches updates from all remotes configured.
bash
git fetch [remote]
git fetch [remote] [branch]
git fetch --all
💻

Example

This example shows how to fetch updates from the remote named origin. It downloads new commits and updates remote-tracking branches without changing your current working files.

bash
git fetch origin
Output
From https://github.com/user/repo * branch main -> origin/main * [new branch] feature-x -> origin/feature-x
⚠️

Common Pitfalls

One common mistake is expecting git fetch to update your working files or merge changes automatically. It only downloads data. To apply changes, you must merge or rebase manually.

Wrong way:

git fetch origin
# Expecting local files to update automatically (they won't)

Right way:

git fetch origin
git merge origin/main
📊

Quick Reference

CommandDescription
git fetchDownload updates from default remote
git fetch originFetch updates from remote named origin
git fetch origin mainFetch only the main branch from origin
git fetch --allFetch updates from all remotes
git fetch --pruneRemove remote-tracking branches that no longer exist on remote

Key Takeaways

Use git fetch to download remote changes without affecting your local files.
After fetching, manually merge or rebase to apply changes to your working branch.
Specify remote and branch to fetch only what you need.
Use git fetch --all to update all remotes at once.
Remember git fetch does not change your working directory automatically.