Git Notes
Learn how git fetch downloads remote changes without modifying your working directory. Understand fetch vs pull, remote tracking branches, and fetch strategies.
Git fetch downloads commits, files, and refs from a remote repository into your local repo without modifying your working directory or current branch. It is a safe way to review changes before integrating them.
Fetch vs Pull
| Aspect | git fetch | git pull |
|---|---|---|
| Downloads changes | ✅ Yes | ✅ Yes |
| Modifies working dir | ❌ No | ✅ Yes |
| Merges automatically | ❌ No | ✅ Yes |
| Can cause conflicts | ❌ No | ✅ Possible |
| Safe to run anytime | ✅ Yes | ⚠️ May disrupt work |
Basic Fetch Commands
Fetch from Default Remote (origin)
git fetchremote: Enumerating objects: 12, done. remote: Counting objects: 100% (12/12), done. remote: Compressing objects: 100% (8/8), done. remote: Total 12 (delta 4), reused 8 (delta 3) Unpacking objects: 100% (12/12), done. From https://github.com/user/repo a1b2c3d..e5f6g7h main -> origin/main * [new branch] feature/x -> origin/feature/x
Fetch from a Specific Remote
git fetch origin
git fetch upstreamFetch All Remotes
git fetch --allFetching origin Fetching upstream From https://github.com/original/repo f7e8d9c..a1b2c3d main -> upstream/main
Fetch a Specific Branch
git fetch origin mainWhat Happens After Fetch
After fetching, remote tracking branches are updated:
# Your local branches (unchanged)
git branch* feature/auth main
# Remote tracking branches (updated by fetch)
git branch -rorigin/main ← Updated by fetch origin/feature/auth origin/feature/new ← New branch from fetch
# Compare your branch with fetched remote
git log main..origin/main --onelinee5f6g7h Update API documentation d4e5f6g Add error handling middleware c3d4e5f Fix authentication bug
These commits exist on the remote but not in your local main yet.
Reviewing Fetched Changes
# See what changed on remote main
git log origin/main --oneline -5e5f6g7h (origin/main) Update API documentation d4e5f6g Add error handling middleware c3d4e5f Fix authentication bug b2c3d4e Refactor user service a1b2c3d (HEAD -> main) Initial commit
# See diff between local and remote
git diff main origin/main# See commit details
git show origin/mainIntegrating Fetched Changes
After reviewing, merge or rebase:
# Option 1: Merge
git checkout main
git merge origin/main
# Option 2: Rebase
git checkout main
git rebase origin/mainFetch with Prune
Remove local references to deleted remote branches:
git fetch --prune
# or
git fetch -pFrom https://github.com/user/repo - [deleted] (none) -> origin/feature/old-branch - [deleted] (none) -> origin/feature/merged-branch a1b2c3d..e5f6g7h main -> origin/main
Auto-Prune on Every Fetch
git config --global fetch.prune trueFetch Tags
# Fetch all tags
git fetch --tags
# Fetch without tags
git fetch --no-tagsShallow Fetch
# Fetch only recent history
git fetch --depth 1 origin main
# Deepen an existing shallow clone
git fetch --deepen 10Practical Workflows
Safe Update Check
# Check for updates without disrupting work
git fetch origin
# See if main has moved ahead
git log HEAD..origin/main --oneline
# If output is empty, you are up to date
# See if your branch is behind
git statusOn branch feature/auth Your branch is behind 'origin/feature/auth' by 3 commits, and can be fast-forwarded.
Pre-Merge Review
# Before merging a PR locally
git fetch origin
git log origin/feature/new-feature --oneline -10
git diff main..origin/feature/new-feature --statInterview Questions
- What is the difference between git fetch and git pull?
git fetch downloads remote changes and updates remote tracking branches without modifying your working directory. git pull does a fetch followed by a merge (or rebase), which modifies your current branch.
- Why would you use git fetch instead of git pull?
Fetch is safer — it lets you review changes before integrating them. You can see what others have pushed, compare branches, and decide how to merge without risking conflicts in your working directory.
- What are remote tracking branches?
Remote tracking branches (like origin/main) are local references that track the state of branches on remote repositories. They are updated by git fetch and show where remote branches were at the last fetch.
- What does
git fetch --prunedo?
It removes local remote-tracking references for branches that no longer exist on the remote server, keeping your local reference list clean.
- Does git fetch modify any local branches?
No. Git fetch only updates remote tracking branches (refs/remotes/). Your local branches, working directory, and staging area remain completely untouched.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Fetch - Download Remote Changes Without Merging.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, working, with, repositories, fetch
Related Git & GitHub Topics