Pulling Changes from Remote
The git pull command downloads changes from a remote repository and integrates them with your local branch.
Basic Pull
bash
1git pull <remote> <branch>
2
3# Example
4git pull origin mainWhat git pull Does
git pull is actually two commands in one:
bash
1git pull origin main
2
3# Is equivalent to:
4git fetch origin main
5git merge origin/mainPull Options
Pull Current Branch
bash
1# Pull the tracked remote branch
2git pullPull with Rebase
bash
1# Rebase instead of merge
2git pull --rebase origin main
3
4# Or short form
5git pull -r origin mainMerge vs Rebase:
Merge (default): Rebase:
A─B─C─────M (merge) A─B─C─D'─E' (linear)
\ /
D─E
Pull All Branches
bash
1git fetch --allUnderstanding Pull
Scenario: Remote Has New Commits
Your Local: A ─ B ─ C
Remote: A ─ B ─ C ─ D ─ E
After git pull:
Your Local: A ─ B ─ C ─ D ─ E
Scenario: Both Have New Commits
Your Local: A ─ B ─ C ─ X
Remote: A ─ B ─ C ─ D
After git pull (merge):
Your Local: A ─ B ─ C ─ X ─ M
\ /
D
Handling Pull Conflicts
When your changes conflict with remote changes:
bash
1git pull origin main
2# CONFLICT (content): Merge conflict in file.txt
3# Automatic merge failed; fix conflicts and then commitResolve Conflicts
- Open the conflicting file(s)
- Find conflict markers:
<<<<<<< HEAD
Your changes
=======
Remote changes
>>>>>>> origin/main
- Edit to keep what you want
- Remove conflict markers
- Stage and commit:
bash
1git add file.txt
2git commit -m "Resolve merge conflict"Fetch vs Pull
| Command | Action |
|---|---|
git fetch | Download changes (don't merge) |
git pull | Download AND merge changes |
When to Use Fetch
bash
1# See what's new without merging
2git fetch origin
3
4# Review changes
5git log HEAD..origin/main
6
7# Then decide to merge
8git merge origin/mainPull Best Practices
1. Pull Before Starting Work
bash
1# Start of day
2git pull origin main
3# Now start coding2. Pull Before Push
bash
1git pull origin main
2git push origin main3. Use Rebase for Cleaner History
bash
1# Configure rebase as default
2git config --global pull.rebase true
3
4# Or per-pull
5git pull --rebase4. Stash Changes If Needed
bash
1# If you have uncommitted changes
2git stash
3git pull origin main
4git stash popCommon Pull Workflows
Simple Update
bash
1git pull origin mainUpdate Feature Branch
bash
1# On feature branch
2git checkout feature/my-feature
3
4# Get latest main
5git checkout main
6git pull origin main
7
8# Update feature with latest main
9git checkout feature/my-feature
10git merge main
11# Or: git rebase mainFork Sync
bash
1# Fetch from upstream
2git fetch upstream
3
4# Merge upstream changes
5git merge upstream/main
6
7# Push to your fork
8git push origin mainSummary
| Command | Description |
|---|---|
git pull | Pull current branch's upstream |
git pull origin main | Pull main from origin |
git pull --rebase | Pull with rebase |
git fetch | Download without merging |
Practice pulling in the next exercise!
