#git-fetch-vs-pull
What is the difference between `git fetch` and `git pull`?
Что отвечать
`git fetch` pulls commits from the remote and updates the remote-tracking branches (for example `origin/main`), but **it does not move your local branch**. `git pull` is `git fetch` plus an immediate attempt to combine those commits with your local branch. By default it does this through a merge, which creates a merge commit when main and your branch have diverged. Many teams set `git config pull.rebase true` so that pull does fetch + rebase instead of merge, which keeps history cleaner.
Что хотят услышать
The candidate should: - say that fetch is safer: you see what arrived, then you decide on merge or rebase - explain that `pull --rebase` rebases **only your own local commits on top of the fresh remote**. It does not rewrite anyone else's remote commits. This is a common misconception. - name `git config --global pull.rebase true` as a sensible default, and `git config --global pull.ff only` as an even stricter option (fast-forward only, otherwise an error) - mention `git pull --autostash` for the case where you have uncommitted changes
Подводные камни
- ✗ Claiming that `pull --rebase` rewrites remote commits. In reality only your own local commits are replayed on top of the remote, and other people's commits are left untouched.
- ✗ Thinking `git pull` with no arguments always pulls the current branch. It needs a tracking branch, otherwise you have to run `git pull origin main`.
- ✗ Running `git pull` on top of a dirty working tree. The merge will fail, so stash first or use autostash.
Follow-up
- ? What does `git log HEAD..origin/main` show after a fetch?
- ? How does `pull.ff only` differ from `pull.rebase true`?
- ? How do you undo a botched `git pull`?
Глубина в базе знаний