my-notes

git_fetch_vs_pull

1 min read

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 main from origin.


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 pull always 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 is Safe

Fetch never changes your working files. Use it to preview.

Pull = Fetch + Merge

git pull is literally git fetch followed by git merge.

Prefer Rebase for Feature Branches
git pull --rebase origin main


#git #fetch #pull #remote #sync

Built with mdgarden