Skip
Arish's avatar

18. Exercise - Renaming Branches


Exercise: Renaming Branches

Practice renaming branches in various scenarios.

Setup

bash
1mkdir rename-practice
2cd rename-practice
3git init
4
5echo "# Rename Practice" > README.md
6git add README.md
7git commit -m "Initial commit"
8
9# Create branches with "mistakes"
10git branch feture-login      # Typo
11git branch feature1          # No description
12git branch my-branch         # Not following convention
13git branch user-profile      # Missing prefix

Exercise 1: Fix a Typo

Task

Rename feture-login to feature-login.

Steps

bash
1# Rename from anywhere
2git branch -m feture-login feature-login
3
4# Verify
5git branch

Expected Output

feature-login feature1 * main my-branch user-profile

Exercise 2: Add Description to Branch Name

Task

Rename feature1 to feature-payment-gateway.

Steps

bash
1# Rename the branch
2git branch -m feature1 feature-payment-gateway
3
4# Verify
5git branch

Exercise 3: Rename Current Branch

Task

Switch to my-branch and rename it to feature-navigation.

Steps

bash
1# Switch to the branch
2git checkout my-branch
3
4# Rename current branch (no old name needed)
5git branch -m feature-navigation
6
7# Verify
8git branch

Expected Output

feature-login * feature-navigation feature-payment-gateway main user-profile

Exercise 4: Add Prefix to Branch

Task

Rename user-profile to feature/user-profile following Git Flow convention.

Steps

bash
1# Rename with prefix
2git branch -m user-profile feature/user-profile
3
4# Verify
5git branch

Exercise 5: Rename Multiple Branches

Task

Standardize all feature branches to use feature/ prefix.

Steps

bash
1# Rename each branch
2git branch -m feature-login feature/login
3git branch -m feature-navigation feature/navigation
4git branch -m feature-payment-gateway feature/payment-gateway
5
6# Verify all branches
7git branch

Expected Output

feature/login feature/navigation feature/payment-gateway feature/user-profile * main

Exercise 6: Handle Existing Name Conflict

Task

Try to rename a branch to a name that already exists.

Steps

bash
1# Create a branch
2git branch temp-feature
3
4# Try to rename to existing branch
5git branch -m temp-feature feature/login
6# Error: A branch named 'feature/login' already exists
7
8# Force rename (this will delete the old feature/login!)
9# git branch -M temp-feature feature/login
10
11# Better: Choose a different name
12git branch -m temp-feature feature/temp
13
14# Verify
15git branch

Challenge: Complete Branch Reorganization

Task

Reorganize branches to follow this convention:

  • Features: feature/description
  • Bugfixes: bugfix/description
  • Hotfixes: hotfix/description

Setup

bash
1# Create branches to reorganize
2git branch login-fix
3git branch urgent-security-patch
4git branch new-dashboard
5git branch fix-header-bug

Solution

bash
1# Rename feature branch
2git branch -m new-dashboard feature/dashboard
3
4# Rename bugfix branches
5git branch -m login-fix bugfix/login
6git branch -m fix-header-bug bugfix/header
7
8# Rename hotfix branch
9git branch -m urgent-security-patch hotfix/security-patch
10
11# Verify
12git branch

Expected Output

bugfix/header bugfix/login feature/dashboard feature/login feature/navigation feature/payment-gateway feature/temp feature/user-profile hotfix/security-patch * main

Self-Check Questions

  1. What flag is used to rename a branch?
  2. How do you rename the current branch?
  3. How do you rename a branch you're not on?
  4. What does -M (uppercase) do?

Answers

  1. -m flag: git branch -m new-name
  2. Just: git branch -m new-name (no old name needed)
  3. git branch -m old-name new-name
  4. Force rename (overwrites existing branch)

Summary

You've practiced:

  • ✅ Fixing typos in branch names
  • ✅ Adding descriptions to branch names
  • ✅ Renaming current branch
  • ✅ Adding prefixes for organization
  • ✅ Handling naming conflicts

Next, let's learn about deleting branches!