What is the difference between git fetch and git pull?
Learn the difference between git fetch and git pull: fetch downloads remote changes safely, pull fetches and merges in one step. Know when to use each.
Expected Interview Answer
git fetch downloads new commits and refs from a remote into your local repository without changing your working files, while git pull does a fetch and then immediately merges (or rebases) those changes into your current branch.
Fetch is a safe, read-only sync: it updates your remote-tracking branches (like origin/main) so you can inspect what changed with git log or git diff before integrating anything. Pull is essentially git fetch followed by git merge FETCH_HEAD, so it can move your working branch and create merge commits or conflicts in one step. In short, git pull equals git fetch plus an automatic integration step.
- Fetch lets you review incoming changes before integrating
- Fetch never touches your working directory, so it is always safe
- Pull saves a step by fetching and integrating together
- Fetch helps avoid surprise merge conflicts
- Understanding both gives you control over how remote work is merged
AI Mentor Explanation
Fetch is like receiving the opposition's latest team sheet and studying it in the pavilion before you decide anything — your own lineup stays untouched. Pull is like receiving that team sheet and immediately reshuffling your batting order to match, all in one motion. Fetch gives you time to scout; pull commits you to react instantly, which can backfire if the changes clash with your plan.
Step-by-Step Explanation
Step 1
Fetch updates tracking refs
git fetch origin downloads new commits and updates remote-tracking branches like origin/main without touching your files.
Step 2
Review the changes
Use git log HEAD..origin/main or git diff to inspect exactly what arrived before integrating.
Step 3
Integrate manually if desired
Run git merge origin/main or git rebase origin/main when you are ready to apply the fetched work.
Step 4
Pull combines both
git pull runs a fetch and then immediately merges (or rebases with --rebase) the changes into your current branch.
Step 5
Handle conflicts
Because pull integrates instantly, it can surface merge conflicts you must resolve on the spot.
What Interviewer Expects
- Fetch downloads changes without integrating them
- Pull equals fetch plus merge (or rebase)
- Awareness that pull can move your branch and cause conflicts
- Knowing fetch updates remote-tracking branches like origin/main
- When fetching first is safer than pulling
Common Mistakes
- Thinking git fetch updates your working files or current branch
- Believing git pull is always safe with no conflict risk
- Not knowing that git pull can be configured to rebase instead of merge
- Confusing origin/main (tracking ref) with your local main
Best Answer (HR Friendly)
“Fetch just downloads the latest changes from the shared repository so you can look at them, without touching your own work. Pull downloads those changes and immediately merges them into what you are working on. Fetch is the cautious option; pull is the one-step shortcut.”
Code Example
# Download remote changes without altering your working branch
git fetch origin
# See what came in before integrating
git log HEAD..origin/main --oneline
# Integrate manually when ready
git merge origin/main# Equivalent to: git fetch + git merge origin/main
git pull origin main
# Or fetch and rebase instead of merge
git pull --rebase origin mainFollow-up Questions
- What is a remote-tracking branch like origin/main?
- How does git pull --rebase differ from a plain git pull?
- How can you inspect changes after a fetch but before merging?
- What does FETCH_HEAD point to?
- Why might fetching first help you avoid conflicts?
MCQ Practice
1. What does git fetch do?
git fetch downloads new commits and updates remote-tracking branches, but it does not modify your working files or current branch.
2. git pull is roughly equivalent to which combination?
git pull performs a git fetch and then merges (or rebases) the fetched changes into the current branch.
3. Why might you prefer git fetch over git pull?
Fetch is read-only for your working branch, so you can inspect what changed and decide how to integrate, avoiding surprise conflicts.
Flash Cards
What does git fetch update? — Remote-tracking branches like origin/main, without touching your working files.
What is git pull equivalent to? — git fetch followed by git merge (or git rebase) into your current branch.
Is git fetch safe to run anytime? — Yes — it never changes your working directory or current branch.
How do you make pull rebase instead of merge? — Use git pull --rebase.