Module 03: Branching Basics
Learning Objectives
In this module, you will:
- Understand what a branch is in Git
- Create new branches using
git branchorgit switch -c - Switch between branches using
git switch - View all branches with
git branch - Understand that branches allow parallel development
Challenge
Setup
Run the setup script to create your challenge environment:
.\setup.ps1
This will create a challenge/ directory with a Git repository that has some initial commits on the main branch.
Your Task
Your goal is to create a feature branch, make commits on it, and understand how branches work independently.
Steps:
- Create a new branch called
feature-login - Switch to the new branch
- Create a new file
login.pywith some login functionality - Commit the new file to your feature branch
- Make another change to
login.pyand commit it - Switch back to the main branch and observe that
login.pydoesn't exist there
Suggested Approach:
- Navigate to the challenge directory:
cd challenge - View existing branches:
git branch - Create and switch to new branch:
git switch -c feature-login - Create
login.pywith any content you like - Stage and commit:
git add login.pyandgit commit -m "Add login functionality" - Modify
login.py, then commit again - Switch back to main:
git switch main - Run
ls(or check your file explorer) and notice thatlogin.pydoesn't exist on main! - Switch back to feature-login:
git switch feature-login - Run
ls(or check your file explorer) again and see thatlogin.pyis back!
Important Notes:
- Use
git switchto change branchesgit switch -c <name>creates and switches in one command- Branches are independent - files in one branch don't affect another until you merge
- You can switch between branches as many times as you want
Key Concepts
- Branch: A lightweight movable pointer to a commit. Branches allow you to work on different features independently.
- HEAD: A pointer to the current branch you're working on. When you switch branches, HEAD moves.
- main/master: The default branch name in Git (main is the modern convention, master is older).
- Feature Branch: A branch created to develop a specific feature, separate from the main codebase.
Useful Commands
git branch # List all branches (* shows current)
git branch <name> # Create a new branch
git switch <branch> # Switch to an existing branch
git switch -c <name> # Create and switch to new branch
git switch - # Switch back to previous branch
git branch -d <name> # Delete a branch (we won't use this yet)
Verification
Once you've completed the challenge, verify your solution:
.\verify.ps1
The verification script will check that you've created the branch, made commits, and that the branches are independent.
Need to Start Over?
If you want to reset the challenge and start fresh:
.\reset.ps1
This will remove the challenge directory and run the setup script again, giving you a clean slate.