git fetch Command Guide
Download objects and refs from another repository
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git fetchFetch from origingit fetch <remote>Fetch from specificgit fetch --allFetch all remotesOptions
--pruneRemove stale refs--tagsFetch all tags--depth=1Shallow fetchAfter Fetch
git log HEAD..origin/mainSee new commitsgit merge origin/mainMerge fetchedgit rebase origin/mainRebase on fetchedDownloadable Image Preview
Basic Usage
The git fetch command downloads commits, files, and refs from a remote repository into your local repo. Unlike pull, it doesn't merge changes.
# Fetch from default remote (origin)
git fetch
# Fetch from specific remote
git fetch upstream
# Fetch all remotes
git fetch --allFetch vs Pull
git fetch
- • Downloads changes only
- • Doesn't modify working directory
- • Safe - lets you review first
- • You decide when to merge
git pull
- • Downloads AND merges
- • Modifies your branch
- • Quick but less control
- • Equals: fetch + merge
# These are equivalent:
git pull origin main
# And:
git fetch origin main
git merge origin/maingit fetch when you want to see what changed before integrating. It's the safer choice for reviewing changes.Common Options
| Option | Description |
|---|---|
| --all | Fetch from all remotes |
| --prune, -p | Remove stale remote-tracking refs |
| --tags, -t | Fetch all tags |
| --depth=<n> | Limit history depth |
| --dry-run | Show what would be fetched |
Reviewing Fetched Changes
# After fetching, see what's new
git fetch origin
# See new commits
git log HEAD..origin/main
git log --oneline HEAD..origin/main
# See the diff
git diff HEAD origin/main
# See changed files
git diff --stat HEAD origin/main
# Then merge when ready
git merge origin/main
# Or rebase instead
git rebase origin/mainPractical Examples
# Daily workflow: fetch, review, merge
git fetch origin
git log --oneline HEAD..origin/main
git merge origin/main
# Fetch and prune deleted branches
git fetch --prune
# Fetch from fork's upstream
git fetch upstream
git merge upstream/main
# Quick check if remote has updates
git fetch --dry-run
# Fetch specific branch
git fetch origin feature-branchFrequently Asked Questions
What is the difference between fetch and pull?
Fetch downloads changes but doesn't integrate them - your working directory stays unchanged. Pull is fetch + merge in one step. Fetch is safer for reviewing before merging.
Where do fetched changes go?
They go to remote-tracking branches (like origin/main). These are local copies of remote branches. Your local branches are not changed until you merge or rebase.
How do I see what was fetched?
Use "git log HEAD..origin/main" to see commits that are in origin/main but not in your current branch. Or "git diff HEAD origin/main" for changes.
Should I use fetch or pull?
Use fetch when you want to review changes before integrating, or when you might want to rebase instead of merge. Use pull for quick updates when you trust the incoming changes.
Summary
git fetch downloads changes safely without modifying your work. Review what's new, then merge when ready.
Quick Reference
git fetch- Fetch from origingit fetch --all- Fetch all remotesgit fetch --prune- Remove stale refsgit log HEAD..origin/main- See new commits
Official Documentation
For authoritative information, refer to the official documentation: