FresherTechnicalGit
What is the difference between git fetch and git pull?
Try answering first
Model answer
git fetch downloads new commits and updates your remote-tracking branches such as origin/main, but it does not touch your local branches or working files. git pull is git fetch followed by an integration step, a merge by default or a rebase if you ask for it.
git fetch origin # safe, only updates origin/*
git log HEAD..origin/main # inspect incoming commits
git merge origin/main # integrate when ready
git pull # fetch + merge in one step
git pull --rebase # fetch + rebase
Fetch first when you want to see what changed before it lands in your branch. Pull is a convenience when you already know you want the update.
Many teams set git config --global pull.rebase true or pull.ff only so that a pull never creates a surprise merge commit.
Takeaway: fetch is read-only for your work, pull changes your current branch.
Key points
- Fetch updates remote-tracking branches only
- Pull equals fetch plus merge or rebase
- Fetch is safe to run any time
Likely follow-ups
- What does pull --rebase change?
- How do you see commits that fetch brought in?
Common mistakes
- Thinking fetch changes your working files
- Running pull blindly and getting an unexpected merge commit