Skip
Arish's avatar

16. Exercise - Switch Branches


Exercise: Switching Branches

Practice switching between branches and understanding how files change.

Setup

Continue from the previous exercise, or create a fresh setup:

bash
1mkdir switch-practice
2cd switch-practice
3git init
4
5# Create main with a file
6echo "Main content" > main.txt
7git add main.txt
8git commit -m "Initial commit on main"
9
10# Create feature-a with its own file
11git checkout -b feature-a
12echo "Feature A content" > feature-a.txt
13git add feature-a.txt
14git commit -m "Add feature A"
15
16# Create feature-b with its own file
17git checkout main
18git checkout -b feature-b
19echo "Feature B content" > feature-b.txt
20git add feature-b.txt
21git commit -m "Add feature B"

Exercise 1: Basic Branch Switching

Task

Switch between the three branches and observe file changes.

Steps

bash
1# Check current branch
2git branch
3
4# List files
5ls
6
7# Switch to main
8git checkout main
9ls
10# Only main.txt
11
12# Switch to feature-a
13git checkout feature-a
14ls
15# main.txt and feature-a.txt
16
17# Switch to feature-b
18git checkout feature-b
19ls
20# main.txt and feature-b.txt

Observation

Notice how the files change as you switch branches!


Exercise 2: Use git switch

Task

Switch branches using the modern git switch command.

Steps

bash
1# Switch to main
2git switch main
3
4# Switch to feature-a
5git switch feature-a
6
7# Try to switch to non-existent branch
8git switch feature-xyz
9# error: invalid reference: feature-xyz
10
11# Verify current branch
12git switch -
13# Switches to previous branch (like cd -)

Exercise 3: Switch with Uncommitted Changes

Task

Understand what happens with uncommitted changes.

Steps

bash
1# Go to feature-a
2git checkout feature-a
3
4# Make a change but don't commit
5echo "Uncommitted work" >> feature-a.txt
6
7# Try to switch branches
8git checkout main
9# Warning or error about uncommitted changes

Solutions

bash
1# Option 1: Commit the changes
2git add feature-a.txt
3git commit -m "Update feature A"
4git checkout main
5
6# Option 2: Stash the changes
7git stash
8git checkout main
9git checkout feature-a
10git stash pop
11
12# Option 3: Discard changes (careful!)
13git checkout -- feature-a.txt
14git checkout main

Exercise 4: Quick Branch Switching

Task

Learn shortcuts for switching between recent branches.

Steps

bash
1# Switch to main
2git checkout main
3
4# Switch to feature-a
5git checkout feature-a
6
7# Switch back to previous branch (main)
8git checkout -
9
10# Switch back again (feature-a)
11git checkout -

The - refers to the previously checked out branch.


Exercise 5: Check Where You Are

Task

Learn different ways to see your current branch.

Steps

bash
1# Method 1: git branch (shows * next to current)
2git branch
3
4# Method 2: git status
5git status
6# On branch feature-a
7
8# Method 3: git branch --show-current
9git branch --show-current
10# feature-a
11
12# Method 4: Check HEAD
13cat .git/HEAD
14# ref: refs/heads/feature-a

Exercise 6: Switch to Remote Branch

Task

Switch to a branch that exists on remote (simulated).

Steps

bash
1# First, create a "remote" branch locally for practice
2git checkout main
3git checkout -b origin/feature-remote
4git checkout main
5
6# Now pretend you want to track this remote branch
7git checkout -b feature-remote origin/feature-remote
8# Or simply:
9git checkout feature-remote

In real scenarios:

bash
1# Fetch remote branches
2git fetch origin
3
4# Switch to remote branch (creates local tracking branch)
5git checkout remote-branch-name

Challenge: Branch Navigation

Task

Navigate through this sequence correctly:

  1. Start on main
  2. Switch to feature-a
  3. Go back to main
  4. Switch to feature-b
  5. Go to the previous branch
  6. Create and switch to feature-c
  7. Return to main

Solution

bash
1git checkout main        # 1. Start on main
2git checkout feature-a   # 2. Switch to feature-a
3git checkout main        # 3. Go back to main
4git checkout feature-b   # 4. Switch to feature-b
5git checkout -           # 5. Previous branch (main)
6git checkout -b feature-c # 6. Create and switch to feature-c
7git checkout main        # 7. Return to main

Quick Reference

ActionCommand
Switch branchgit checkout branch or git switch branch
Previous branchgit checkout - or git switch -
Current branchgit branch --show-current
List branchesgit branch

Summary

You've practiced:

  • ✅ Switching between branches
  • ✅ Using git checkout and git switch
  • ✅ Handling uncommitted changes
  • ✅ Quick switching with -
  • ✅ Checking current branch

Next, let's learn about renaming branches!