Branching Exercises

Practice Git branching operations with real-world scenarios

Learning Objectives

  • Practice creating and managing multiple branches
  • Learn to handle merging scenarios including conflicts
  • Implement common branching strategies
  • Master branch navigation and history visualization

Practice Overview

These exercises will help you master Git branching through practical scenarios. You'll work with multiple branches, handle merges, and resolve conflicts - all common tasks in real-world development.

Exercise 1: Feature Branch Workflow

Scenario

You're working on a website project. Create feature branches to implement a navigation menu and a contact form, then merge them into the main branch.

Steps:

  1. Create and switch to a branch for the navigation feature
  2. Make changes and commit them
  3. Create another branch for the contact form
  4. Make and commit changes on the contact form branch
  5. Merge both features into the main branch

Solution:

Feature Branch Commands
# Start with navigation feature
$ git checkout -b feature/navigation
$ touch nav.html
$ git add nav.html
$ git commit -m "Add navigation structure"
# Create contact form feature
$ git checkout main
$ git checkout -b feature/contact-form
$ touch contact.html
$ git add contact.html
$ git commit -m "Add contact form"
# Merge features
$ git checkout main
$ git merge feature/navigation
$ git merge feature/contact-form

Exercise 2: Resolving Merge Conflicts

Scenario

Two team members have made different changes to the same file. Practice resolving the resulting merge conflict.

Setup:

Create Conflict Scenario
# Create and modify file in main
$ echo "# Project Title
Welcome to our website." > README.md
$ git add README.md
$ git commit -m "Initial README"
# Create feature branch
$ git checkout -b feature/readme-update
$ echo "# Project Title
Welcome to our awesome project!" > README.md
$ git commit -am "Update README welcome message"
# Switch back and modify main
$ git checkout main
$ echo "# Project Title
Welcome to our professional site." > README.md
$ git commit -am "Update README messaging"

Resolve Conflict:

Conflict Resolution
# Attempt merge
$ git merge feature/readme-update
# Edit the conflicted file
$ git add README.md
$ git commit -m "Merge feature/readme-update: Resolve welcome message"

Conflict Resolution Example:

README.md Conflict
# Project Title
<<<<<<< HEAD
Welcome to our professional site.
=======
Welcome to our awesome project!
>>>>>>> feature/readme-update
# Resolved version:
# Project Title
Welcome to our awesome professional site!

Exercise 3: Branch Management

Scenario

Practice branch management tasks including renaming, deleting, and listing branches. Also learn to visualize branch history.

Tasks:

  1. List all branches and their latest commits
  2. Rename a branch
  3. Delete merged branches
  4. Visualize branch history

Commands:

Branch Management Commands
# List branches with latest commits
$ git branch -v
# Rename a branch
$ git branch -m feature/navigation feature/nav-menu
# Delete merged branches
$ git branch --merged
$ git branch -d feature/nav-menu
$ git branch -d feature/contact-form
# Visualize history
$ git log --graph --oneline --all --decorate

Exercise 4: Advanced Branching Patterns

Scenario

Implement a feature-development-release branch pattern common in larger projects.

Implementation:

Advanced Branch Pattern
# Create development branch
$ git checkout -b develop
$ git push -u origin develop
# Create feature branch from develop
$ git checkout -b feature/user-auth develop
# Work on feature
$ touch auth.js
$ git add auth.js
$ git commit -m "Add user authentication"
# Merge to develop
$ git checkout develop
$ git merge feature/user-auth
# Create release branch
$ git checkout -b release/1.0 develop
# Make release preparations...
$ git commit -am "Prepare for release 1.0"
# Merge to main and develop
$ git checkout main
$ git merge release/1.0
$ git tag -a v1.0 -m "Version 1.0"
$ git checkout develop
$ git merge release/1.0

Branching Best Practices

  • Always create branches for new features or fixes, no matter how small
  • Use descriptive branch names that reflect their purpose
  • Regularly sync your feature branches with their base branch to avoid major conflicts
  • Delete branches after they're merged to keep your repository clean