git_fetch_vs_pull
git fetch vs pull
Understand the difference between fetch and pull.
π Comparison
graph LR
A[Remote] -->|fetch| B[Remote-tracking branches]
B -->|merge| C[Local branch]
A -->|pull| C
| Command | Action |
|---|---|
fetch |
Download only |
pull |
Download + merge |
π₯ git fetch
Fetch from Origin
git fetch origin
Downloads commits, files, and refs from remote. Does NOT change working files.
Fetch All Remotes
git fetch --all
Fetches from all configured remotes.
Fetch Specific Branch
git fetch origin feature-branch
Fetches only the specified branch.
Fetch with Prune
git fetch --prune
Fetches and removes stale remote-tracking branches.
Fetch Tags
git fetch --tags
Downloads all tags from remote.
After Fetch
See What Changed
git log HEAD..origin/main
Shows commits on remote that you don't have.
Compare Changes
git diff HEAD origin/main
Shows diff between your HEAD and remote.
Merge After Fetch
git merge origin/main
Merges fetched changes into current branch.
Rebase After Fetch
git rebase origin/main
Rebases your commits onto fetched changes.
π₯ git pull
Pull from Tracking Branch
git pull
Fetches and merges from upstream tracking branch.
Pull from Specific Remote
git pull origin main
Fetches and merges
mainfromorigin.
Pull with Rebase
git pull --rebase
Fetches and rebases instead of merging (cleaner history).
Pull Specific Branch
git pull origin feature-branch
Pulls specific branch from remote.
Pull with Auto-stash
git pull --autostash
Stashes changes before pull, reapplies after.
βοΈ Configure Pull Behavior
Always Rebase on Pull
git config --global pull.rebase true
Makes
git pullalways rebase.
Fast-Forward Only
git config --global pull.ff only
Pull fails if fast-forward not possible.
Merge on Pull (Default)
git config --global pull.rebase false
Uses merge (default behavior).
π When to Use Which
| Scenario | Use |
|---|---|
| See what's new | fetch |
| Review before merge | fetch then log/diff |
| Quick sync | pull |
| Clean history | pull --rebase |
| CI/CD scripts | fetch + explicit merge |
π‘ Tips
Fetch never changes your working files. Use it to preview.
git pull is literally git fetch followed by git merge.
git pull --rebase origin main
π Related
#git #fetch #pull #remote #sync