Understanding Git Pull¶
You're working on a project. Your teammate pushed changes. You need them. You type git pull.
What just happened?
Git pull does two things: fetch + merge. It grabs the latest code from the remote and merges it into your current branch.
One command. Two operations.
What Git Pull Does¶
flowchart LR
A[Remote Repository] -->|1. Fetch| B[Local Repository]
B -->|2. Merge| C[Working Directory]
style A fill:#fff3e0
style B fill:#e1f5ff
style C fill:#e8f5e9
Step 1: Fetch - Download commits from remote
Step 2: Merge - Combine them with your local branch
It's git fetch + git merge in one command.
The Daily Command¶
This is what you'll use most:
That's it. Git pulls from the current branch's remote tracking branch.
You're on main, it pulls from origin/main.
You're on feature/login, it pulls from origin/feature/login.
More explicit version:
This pulls from origin/main specifically. Use this when you want to be clear about what you're pulling.
Pull vs Fetch¶
| Command | What It Does | When to Use |
|---|---|---|
git fetch |
Downloads commits, doesn't merge | When you want to check changes first |
git pull |
Downloads commits AND merges | When you trust the changes and want them now |
Think of it like this:
git fetch→ "Show me what's new"git pull→ "Give me what's new and merge it"
flowchart TB
A[Remote has new commits] --> B{What do you want?}
B -->|Just look| C[git fetch]
B -->|Get and merge| D[git pull]
C --> E[Review changes]
E --> F[Manually merge later]
D --> G[Automatic merge]
style C fill:#e1f5ff
style D fill:#e8f5e9
Common Workflows¶
Morning Routine¶
You start your day. You need the latest code.
Now you're synced. Everyone's changes from yesterday are in your local repo.
Before Starting New Work¶
You're about to create a feature branch. Make sure main is up to date first.
You branched from the latest code. No surprises later.
Updating Your Feature Branch¶
You've been working for days. Main has moved forward. You need those changes.
Or use rebase (cleaner history):
Quick Update While Working¶
You're on a feature branch. You want the latest from that branch's remote.
Git automatically pulls from the tracking branch.
Quick Command Reference¶
| Task | Command |
|---|---|
| Pull from current tracking branch | git pull |
| Pull from specific remote/branch | git pull origin main |
| Pull with rebase instead of merge | git pull --rebase |
| Pull all branches | git pull --all |
| Pull and don't commit merge | git pull --no-commit |
| Pull and show what changed | git pull -v |
Advanced Usage¶
Everything below is for specific situations.
Pull with Rebase (Cleaner History)
Normal git pull creates a merge commit. It's messy.
Standard pull:
Creates:
Pull with rebase:
Creates:
Linear history. No merge commit. Cleaner.
Make it default:
Now git pull will always rebase instead of merge.
Pull from Different Remote
You have multiple remotes. You want to pull from a specific one.
Or pull from a teammate's fork:
Pull All Remotes
You have multiple remotes configured. You want to fetch all of them.
This fetches from all remotes but only merges the current branch's tracking branch.
More control:
Pull Without Auto-Merge
You want to pull but review changes before merging.
Git fetches and attempts to merge but doesn't commit. You can review and then:
Pull and Overwrite Local Changes (Force)
You messed up. You want to throw away local changes and pull fresh.
This Deletes Your Local Work
Make sure you don't need your local changes.
Your local main now matches origin/main exactly. Local changes are gone.
Less destructive version:
Pull Specific Files
You don't want to pull everything. Just specific files.
Can't do it with pull. But you can do it with fetch + checkout:
This updates only that file from the remote.
Real-World Scenarios¶
Scenario 1: Merge Conflict During Pull¶
You pull. Git says there's a conflict.
Output:
Auto-merging src/app.js
CONFLICT (content): Merge conflict in src/app.js
Automatic merge failed; fix conflicts and then commit the result.
What to do:
- Open the conflicted file
- Look for conflict markers:
<<<<<<< HEAD
// Your code
function login() { }
=======
// Their code
function authenticate() { }
>>>>>>> origin/main
- Fix it manually:
- Stage and commit:
Done. Conflict resolved.
Scenario 2: You Have Uncommitted Changes¶
You try to pull but Git blocks you:
Output:
error: Your local changes to the following files would be overwritten by merge:
src/app.js
Please commit your changes or stash them before you merge.
Option 1: Commit your changes
Option 2: Stash your changes
Your changes are back. If there's a conflict, fix it manually.
Scenario 3: Pull Failed Midway¶
The pull started but failed. Now you're stuck.
Output:
Abort the merge:
Back to where you started. Try again or fix the issue.
Common Mistakes¶
Pulling Without Committing Local Work
Wrong:
Right:
Or stash:
Pulling the Wrong Branch
You're on main but you pull from develop:
Now main has develop code. Fix it:
Pulling Without Checking Current Branch
Always check first:
Best Practices¶
Do This
- Check your branch before pulling:
git status - Commit or stash changes before pulling
- Pull from
mainbefore starting new work - Use
git pull --rebasefor cleaner history - Review merge conflicts carefully
Daily Workflow
Start every work session:
You're synced. No surprises.
Fast-Forward vs Merge¶
When you pull, Git might "fast-forward" or create a merge commit.
Fast-forward (no merge commit):
Before pull:
* Your local main
|
* Commit A
After pull (fast-forward):
* Remote commit C
* Remote commit B
* Commit A
Your branch just moves forward. No merge needed.
Merge commit (you had local commits):
Before pull:
* Your local commit
|
* Commit A
After pull:
* Merge commit
|\
| * Remote commit
* | Your local commit
|/
* Commit A
Git creates a merge commit to combine both histories.
Pull vs Clone¶
| Command | What It Does | When to Use |
|---|---|---|
git clone |
Copy entire repository for first time | When you don't have the repo yet |
git pull |
Update existing repository | When you already have the repo |
You clone once. You pull many times.
What's Next?¶
You understand git pull. It's fetch + merge in one command.
Next up: git push. How to send your changes to the remote. What happens when you push. When to force push (and when not to).
But now you know: git pull gets the latest code and merges it into your branch.