Exercise: Pulling Changes
Practice pulling changes from remote repositories.
Setup
For these exercises, you'll simulate having changes on remote by editing files directly on GitHub.
Exercise 1: Make Remote Change on GitHub
Task
Create a change directly on GitHub to pull down.
Steps
- Go to your repository on GitHub
- Click on
README.md - Click the pencil icon (Edit)
- Add a line:
## Updated on GitHub - Commit directly to main
Now Pull Locally
bash
1cd portfolio-project
2
3# Check current status
4git status
5
6# Pull the changes
7git pull origin main
8
9# Verify the change
10cat README.md
11# Should show "## Updated on GitHub"Exercise 2: Fetch vs Pull
Task
Understand the difference between fetch and pull.
Make Another GitHub Change
- On GitHub, edit
README.md - Add another line:
### This is a fetch test - Commit to main
Fetch (Download Only)
bash
1# Fetch without merging
2git fetch origin
3
4# Your local file is unchanged
5cat README.md
6# Doesn't show the new line yet
7
8# See what was fetched
9git log HEAD..origin/main --oneline
10# Shows the new commit
11
12# See the diff
13git diff HEAD..origin/main
14
15# Now merge when ready
16git merge origin/main
17
18# Verify
19cat README.mdExercise 3: Pull with Local Changes
Task
Pull when you have uncommitted local changes.
Make Local Uncommitted Change
bash
1echo "Local work in progress" >> notes.txtTry to Pull
bash
1git pull origin main
2# May warn about uncommitted changesSolution: Use Stash
bash
1# Stash local changes
2git stash
3
4# Now pull
5git pull origin main
6
7# Restore local changes
8git stash pop
9
10# Check status
11git statusExercise 4: Pull with Committed Changes
Task
Pull when remote and local both have commits.
Setup
- Make a local commit:
bash
1echo "// Local feature" >> js/main.js
2git add js/main.js
3git commit -m "Add local feature"- On GitHub, edit another file (e.g., add comment to style.css)
- Commit on GitHub
Pull
bash
1# Pull will merge remote changes
2git pull origin main
3
4# Git creates a merge commit
5# View the merge
6git log --oneline --graph -5Exercise 5: Pull with Rebase
Task
Use rebase instead of merge when pulling.
Setup
- Make a local commit:
bash
1echo "// Another local change" >> js/main.js
2git add js/main.js
3git commit -m "Add another local change"- Make a change on GitHub to a different file
Pull with Rebase
bash
1# Pull with rebase
2git pull --rebase origin main
3
4# Your commit is replayed on top
5git log --oneline -5
6# History is linear, no merge commitExercise 6: Handle Pull Conflict
Task
Resolve a conflict during pull.
Create Conflicting Changes
- Locally, edit line 1 of README.md:
bash
1# Edit README.md - change the title
2sed -i '' '1s/.*/# My Awesome Portfolio/' README.md # Mac
3# or manually edit the first line
4git add README.md
5git commit -m "Update local title"- On GitHub, edit the SAME line differently
- Change first line to "# My GitHub Portfolio"
- Commit on GitHub
Pull and Resolve
bash
1git pull origin main
2# CONFLICT!
3
4# Check status
5git status
6# both modified: README.md
7
8# Open README.md and resolve
9# Remove conflict markers, keep what you want
10
11# Complete the merge
12git add README.md
13git commit -m "Resolve README title conflict"
14
15# Push the resolution
16git push origin mainExercise 7: Configure Default Pull Behavior
Task
Set up your preferred pull behavior.
Option 1: Always Rebase
bash
1# Global setting
2git config --global pull.rebase true
3
4# Now 'git pull' uses rebase by defaultOption 2: Always Merge
bash
1git config --global pull.rebase falseOption 3: Fast-Forward Only
bash
1git config --global pull.ff only
2# Fails if can't fast-forwardSummary
You've practiced:
- ✅ Basic pulling
- ✅ Fetch vs pull difference
- ✅ Pulling with uncommitted changes (stash)
- ✅ Pull with merge
- ✅ Pull with rebase
- ✅ Resolving pull conflicts
- ✅ Configuring pull behavior
Continue to Project Task 3!
