What is the difference between git fetch and git pull?
- Technology:
- Git
- Roles:
- Frontend Developer,
- Backend Developer,
- Full Stack Developer,
- Software Engineer,
- DevOps Engineer,
- QA Engineer,
- Mobile Developer,
- Data Engineer,
- Machine Learning Engineer
- Topics:
- Git
Quick answer
git fetch downloads new commits from the remote without changing your working branch; git pull runs a fetch and then merges (or rebases) those commits into your current branch.
Detailed explanation
git fetch updates your remote-tracking branches such as origin/main. Your own branch and working files are untouched, so it is always safe. You can then inspect what changed with git log main..origin/main before deciding how to integrate it.
git pull is git fetch followed by git merge by default. If your branch and the remote have both moved, it creates a merge commit or stops with conflicts. Many teams set git config pull.rebase true so pull replays your local commits on top of the remote instead, which keeps history linear.
A good habit before starting work is to fetch, review, then merge or rebase deliberately, rather than pulling blindly into a branch with uncommitted changes.
Example
git fetch origin
git log --oneline main..origin/main # what is new on the remote?
git merge origin/main # integrate when ready
# or in one step
git pull --rebase origin mainKey points
- fetch: download only, never touches your branch
- pull: fetch + merge (or rebase with --rebase)
- Fetch first when you want to review incoming changes