Skip
Arish's avatar

15. Exercise - Create Branches


Exercise: Creating Branches

Practice creating branches in different scenarios.

Setup

First, create a practice repository:

bash
1# Create and initialize
2mkdir branch-practice
3cd branch-practice
4git init
5
6# Create initial commit
7echo "# Branch Practice" > README.md
8git add README.md
9git commit -m "Initial commit"

Exercise 1: Create a Simple Branch

Task

Create a branch called feature-header.

Steps

bash
1# Create the branch
2git branch feature-header
3
4# Verify it was created
5git branch

Expected Output

feature-header * main

Exercise 2: Create and Switch to a Branch

Task

Create a branch called feature-footer and switch to it.

Steps

bash
1# Create and switch in one command
2git checkout -b feature-footer
3
4# Verify you're on the new branch
5git branch

Expected Output

feature-header * feature-footer main

Exercise 3: Make Changes on a Branch

Task

While on feature-footer, add a file and commit.

Steps

bash
1# Ensure you're on feature-footer
2git checkout feature-footer
3
4# Create a file
5echo "<footer>Copyright 2024</footer>" > footer.html
6
7# Stage and commit
8git add footer.html
9git commit -m "Add footer component"
10
11# Verify the commit
12git log --oneline

Expected Output

abc1234 (HEAD -> feature-footer) Add footer component def5678 (main, feature-header) Initial commit

Exercise 4: Create Branch from Another Branch

Task

Create feature-footer-social from feature-footer.

Steps

bash
1# Ensure you're on feature-footer
2git checkout feature-footer
3
4# Create new branch from current branch
5git checkout -b feature-footer-social
6
7# Verify the branch hierarchy
8git log --oneline --graph --all

Expected Output

* abc1234 (HEAD -> feature-footer-social, feature-footer) Add footer component * def5678 (main, feature-header) Initial commit

Exercise 5: Create Multiple Feature Branches

Task

From main, create three branches for different features.

Steps

bash
1# Switch to main
2git checkout main
3
4# Create multiple branches
5git branch feature-about
6git branch feature-contact
7git branch feature-services
8
9# List all branches
10git branch

Expected Output

feature-about feature-contact feature-footer feature-footer-social feature-header feature-services * main

Exercise 6: Use git switch (Modern Syntax)

Task

Use git switch to create and switch to a new branch.

Steps

bash
1# Create and switch using modern syntax
2git switch -c feature-gallery
3
4# Verify
5git branch --show-current

Expected Output

feature-gallery

Challenge Exercise

Task

Create a branch structure for a team project:

  1. Create develop branch from main
  2. From develop, create:
    • feature/user-auth
    • feature/dashboard
    • feature/settings
  3. Add a commit to each feature branch

Solution

bash
1# Start from main
2git checkout main
3
4# Create develop branch
5git checkout -b develop
6
7# Create feature branches from develop
8git checkout -b feature/user-auth develop
9echo "Auth module" > auth.js
10git add auth.js
11git commit -m "Add auth module"
12
13git checkout -b feature/dashboard develop
14echo "Dashboard" > dashboard.js
15git add dashboard.js
16git commit -m "Add dashboard"
17
18git checkout -b feature/settings develop
19echo "Settings" > settings.js
20git add settings.js
21git commit -m "Add settings"
22
23# View the branch structure
24git log --oneline --graph --all

Self-Check

Verify your work:

bash
1# List all branches
2git branch
3
4# View branch relationships
5git log --oneline --graph --all

You should have branches organized logically with their own commits.

Summary

You've practiced:

  • ✅ Creating branches with git branch
  • ✅ Creating and switching with git checkout -b
  • ✅ Using modern git switch -c
  • ✅ Creating branches from other branches
  • ✅ Managing multiple feature branches

Great work! Next, let's practice switching between branches.