Skip
Arish's avatar

36. How to Pull Changes


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 main

What 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/main

Pull Options

Pull Current Branch

bash
1# Pull the tracked remote branch
2git pull

Pull with Rebase

bash
1# Rebase instead of merge
2git pull --rebase origin main
3
4# Or short form
5git pull -r origin main

Merge vs Rebase:

Merge (default): Rebase: A─B─C─────M (merge) A─B─C─D'─E' (linear) \ / D─E

Pull All Branches

bash
1git fetch --all

Understanding 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 commit

Resolve Conflicts

  1. Open the conflicting file(s)
  2. Find conflict markers:
<<<<<<< HEAD Your changes ======= Remote changes >>>>>>> origin/main
  1. Edit to keep what you want
  2. Remove conflict markers
  3. Stage and commit:
bash
1git add file.txt
2git commit -m "Resolve merge conflict"

Fetch vs Pull

CommandAction
git fetchDownload changes (don't merge)
git pullDownload 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/main

Pull Best Practices

1. Pull Before Starting Work

bash
1# Start of day
2git pull origin main
3# Now start coding

2. Pull Before Push

bash
1git pull origin main
2git push origin main

3. Use Rebase for Cleaner History

bash
1# Configure rebase as default
2git config --global pull.rebase true
3
4# Or per-pull
5git pull --rebase

4. Stash Changes If Needed

bash
1# If you have uncommitted changes
2git stash
3git pull origin main
4git stash pop

Common Pull Workflows

Simple Update

bash
1git pull origin main

Update 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 main

Fork 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 main

Summary

CommandDescription
git pullPull current branch's upstream
git pull origin mainPull main from origin
git pull --rebasePull with rebase
git fetchDownload without merging

Practice pulling in the next exercise!