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:
- Create and switch to a branch for the navigation feature
- Make changes and commit them
- Create another branch for the contact form
- Make and commit changes on the contact form branch
- Merge both features into the main branch
Solution:
# 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 and modify file in main$ echo "# Project TitleWelcome 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 TitleWelcome to our awesome project!" > README.md$ git commit -am "Update README welcome message" # Switch back and modify main$ git checkout main$ echo "# Project TitleWelcome to our professional site." > README.md$ git commit -am "Update README messaging"
Resolve Conflict:
# 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:
# Project Title<<<<<<< HEADWelcome to our professional site.=======Welcome to our awesome project!>>>>>>> feature/readme-update # Resolved version:# Project TitleWelcome 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:
- List all branches and their latest commits
- Rename a branch
- Delete merged branches
- Visualize branch history
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:
# 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