Skip
Arish's avatar

20. Exercise - Deleting Branches


Exercise: Deleting Branches

Practice safely deleting branches in various scenarios.

Setup

bash
1mkdir delete-practice
2cd delete-practice
3git init
4
5# Create initial commit
6echo "# Delete Practice" > README.md
7git add README.md
8git commit -m "Initial commit"
9
10# Create and commit on a feature branch (to be merged)
11git checkout -b feature-merged
12echo "Merged feature" > merged.txt
13git add merged.txt
14git commit -m "Add merged feature"
15
16# Merge it back to main
17git checkout main
18git merge feature-merged
19
20# Create an unmerged branch
21git checkout -b feature-unmerged
22echo "Unmerged feature" > unmerged.txt
23git add unmerged.txt
24git commit -m "Add unmerged feature"
25
26# Create more branches for practice
27git checkout main
28git branch old-feature
29git branch abandoned-work
30git branch temp-testing

Exercise 1: Delete a Merged Branch

Task

Delete the feature-merged branch (it's already merged to main).

Steps

bash
1# Ensure you're on main
2git checkout main
3
4# Delete the merged branch
5git branch -d feature-merged
6
7# Verify
8git branch

Expected Output

Deleted branch feature-merged (was abc1234).

Exercise 2: Try to Delete Unmerged Branch

Task

Try to delete feature-unmerged using safe delete.

Steps

bash
1git checkout main
2git branch -d feature-unmerged

Expected Output

error: The branch 'feature-unmerged' is not fully merged. If you are sure you want to delete it, run 'git branch -D feature-unmerged'.

Git protects you from losing work!


Exercise 3: Force Delete Unmerged Branch

Task

Force delete the feature-unmerged branch.

Steps

bash
1# Force delete
2git branch -D feature-unmerged
3
4# Verify
5git branch

Expected Output

Deleted branch feature-unmerged (was def5678).

Exercise 4: Delete Multiple Branches

Task

Delete old-feature and temp-testing in one command.

Steps

bash
1# Delete multiple branches at once
2git branch -d old-feature temp-testing
3
4# Verify
5git branch

Expected Output

Deleted branch old-feature (was abc1234). Deleted branch temp-testing (was abc1234).

Exercise 5: Try to Delete Current Branch

Task

Attempt to delete the branch you're currently on.

Steps

bash
1# Switch to abandoned-work
2git checkout abandoned-work
3
4# Try to delete it
5git branch -d abandoned-work

Expected Output

error: Cannot delete branch 'abandoned-work' checked out at '/path/to/repo'

Solution

bash
1# Switch to another branch first
2git checkout main
3
4# Now delete it
5git branch -D abandoned-work

Exercise 6: List and Delete Merged Branches

Task

Create several branches, merge some, then delete only the merged ones.

Setup

bash
1# Create and merge branch 1
2git checkout -b feature-a
3echo "A" > a.txt && git add a.txt && git commit -m "Add A"
4git checkout main && git merge feature-a
5
6# Create and merge branch 2
7git checkout -b feature-b
8echo "B" > b.txt && git add b.txt && git commit -m "Add B"
9git checkout main && git merge feature-b
10
11# Create unmerged branch
12git checkout -b feature-c
13echo "C" > c.txt && git add c.txt && git commit -m "Add C"
14git checkout main

Steps

bash
1# List merged branches (excluding main)
2git branch --merged main | grep -v "main"
3
4# Delete merged branches
5git branch --merged main | grep -v "main" | xargs git branch -d
6
7# Verify only unmerged branches remain
8git branch

Expected Output

feature-c * main

Challenge: Cleanup Script

Task

Write a sequence of commands to clean up a repository.

Requirements

  1. Switch to main
  2. List all merged branches
  3. Delete all merged branches (except main)
  4. List remaining branches

Solution

bash
1#!/bin/bash
2# Cleanup script
3
4# Switch to main
5git checkout main
6
7# Show merged branches
8echo "Merged branches to delete:"
9git branch --merged main | grep -v "main"
10
11# Delete them
12git branch --merged main | grep -v "main" | xargs git branch -d
13
14# Show remaining
15echo "Remaining branches:"
16git branch

Self-Check Questions

  1. What's the difference between -d and -D?
  2. Can you delete the branch you're currently on?
  3. How do you delete a remote branch?
  4. How do you list merged branches?

Answers

  1. -d is safe delete (only merged), -D forces delete
  2. No, you must switch to another branch first
  3. git push origin --delete branch-name
  4. git branch --merged

Summary

You've practiced:

  • ✅ Safe delete with -d
  • ✅ Force delete with -D
  • ✅ Deleting multiple branches
  • ✅ Understanding deletion restrictions
  • ✅ Cleaning up merged branches

Great work! You've mastered branch management!