git pull — Fetching and Merging Remote Updates
How git pull works: git fetch + git merge combination, rebase pulls (git pull --rebase), fast-forward only, and preventing unwanted merge commits.
git pull — Fetching and Merging Remote Updates
The git pull command is shorthand for two consecutive actions:
git fetch(download new commits from remote)git merge(orgit rebase) to integrate them into your current local branch.
git pull = git fetch + git merge (or git rebase)1. Syntax and Basic Usage
# Pull changes from default upstream remote branch:
git pull
# Explicitly pull from remote branch:
git pull origin main2. Rebase Pull (git pull --rebase) — Best Practice
By default, running git pull when local and remote branches have diverged generates a messy 3-way merge commit ("Merge branch 'main' of github.com...").
Using --rebase keeps your local commit history completely clean and linear by replaying your local commits on top of incoming remote updates:
git pull --rebase origin mainConfigure Rebase as Global Default:
git config --global pull.rebase true3. Fast-Forward Only Pull (--ff-only)
If you want git pull to fail immediately rather than creating an unexpected merge commit when history diverges:
git pull --ff-only origin mainRelated Topics
git fetch — Downloading Remote Updates Safely
How git fetch works: downloading commits and refs without modifying your local working tree, fetch vs pull, and pruning deleted branches (--prune).
git push — Uploading Local Commits to Remotes
Uploading commits with git push: upstream tracking (-u), safe force pushes (--force-with-lease), and resolving non-fast-forward rejections.