Skip
Arish's avatar

37. Exercise - Pulling Changes


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

  1. Go to your repository on GitHub
  2. Click on README.md
  3. Click the pencil icon (Edit)
  4. Add a line: ## Updated on GitHub
  5. 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

  1. On GitHub, edit README.md
  2. Add another line: ### This is a fetch test
  3. 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.md

Exercise 3: Pull with Local Changes

Task

Pull when you have uncommitted local changes.

Make Local Uncommitted Change

bash
1echo "Local work in progress" >> notes.txt

Try to Pull

bash
1git pull origin main
2# May warn about uncommitted changes

Solution: 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 status

Exercise 4: Pull with Committed Changes

Task

Pull when remote and local both have commits.

Setup

  1. Make a local commit:
bash
1echo "// Local feature" >> js/main.js
2git add js/main.js
3git commit -m "Add local feature"
  1. On GitHub, edit another file (e.g., add comment to style.css)
  2. 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 -5

Exercise 5: Pull with Rebase

Task

Use rebase instead of merge when pulling.

Setup

  1. Make a local commit:
bash
1echo "// Another local change" >> js/main.js
2git add js/main.js
3git commit -m "Add another local change"
  1. 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 commit

Exercise 6: Handle Pull Conflict

Task

Resolve a conflict during pull.

Create Conflicting Changes

  1. 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"
  1. 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 main

Exercise 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 default

Option 2: Always Merge

bash
1git config --global pull.rebase false

Option 3: Fast-Forward Only

bash
1git config --global pull.ff only
2# Fails if can't fast-forward

Summary

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!