Git interview question
What is the difference between git fetch and git pull?
Short answer
git fetch downloads new commits and updates remote-tracking branches like origin/main without touching your working branch. git pull runs a fetch and then integrates the changes into the current branch, by merge by default or by rebase if configured. Fetching first lets you inspect before integrating.
Answer
git fetch contacts the remote, downloads any commits you do not have, and updates your remote-tracking branches (such as origin/main). It never changes your current branch, your working directory or your staged changes, so it is always safe to run. git pull is a convenience command that runs git fetch and then integrates the fetched branch into your current branch, by merge by default, or by rebase if you ask for it or configure it.
What “remote-tracking branch” means
origin/main is your local, read-only snapshot of where main was on the remote at your last fetch. Your own main is a separate branch that only moves when you commit, merge, rebase or reset.
git fetch origin
git status # "Your branch is behind 'origin/main' by 3 commits"
git log --oneline main..origin/main # what's new upstream
git diff main origin/main # the actual changes
After reviewing, you integrate deliberately:
git merge origin/main # or: git rebase origin/main
What pull does
git pull # fetch + merge origin/<upstream> into current branch
git pull --rebase # fetch + rebase your local commits on top
git pull --ff-only # fetch + fast-forward only; refuse if branches diverged
If your branch and the remote have both moved, a plain merge-based pull creates a merge commit such as “Merge branch ‘main’ of github.com:org/repo”, which clutters history. Common team settings:
git config --global pull.rebase true # always rebase local commits on pull
# or
git config --global pull.ff only # never create surprise merges
Since Git 2.27, if you have not configured either and the branches have diverged, Git prints a hint asking you to choose; recent versions refuse to pull divergent branches until you choose.
When I use which
- fetch: before starting work, before a rebase, when reviewing what a teammate pushed, and in CI scripts where you want explicit control.
- pull: quick updates on a branch where I have no local commits, or with
--rebaseon my own feature branch.
In CI pipelines, scripts usually do git fetch plus a git checkout of a specific commit SHA, so the run is reproducible and never creates merges.
Common mistakes
- Running
git pullwith uncommitted changes and being surprised by conflicts. Commit or stash first (git stash, orgit pull --autostashwith rebase). - Thinking
git fetchupdated your branch. It updatedorigin/main, notmain.
What interviewers look for
- Precise description of remote-tracking branches.
- pull = fetch + merge (or rebase).
- Inspecting changes between fetch and integration.
- Knowing the
--rebaseand--ff-onlyoptions and why teams configure them.
A strong answer covers
- fetch downloads commits and updates remote-tracking branches only
- pull is fetch plus merge (or rebase)
- Inspect incoming changes before integrating
- Configures pull behaviour: --rebase, --ff-only, pull.rebase