KandZ – Tuts

We like to help…!

Git 3a 🧩 Understanding Branches

3. Branching and Merging: Managing Parallel Development

Branching is Git’s most powerful feature for collaborative and experimental development, allowing parallel workstreams.

3a Understanding Branches

What are branches?

Imagine you have a big project, like building a house. Instead of having everyone work on the same foundation, branches let you create separate workspaces where different people or teams can build different parts simultaneously.

Think of branches as parallel universes of your code where each one can evolve independently. You can make changes, test ideas, and experiment without worrying about breaking the main project.

Why do we use branches?

  1. Feature Development Without Risk
    When you want to add a new feature like user login, you don’t want to mess up the existing working system. So you create a separate branch where you can build the login functionality completely isolated from the rest of the codebase.
  2. Bug Fixes in Isolation
    If there’s a critical bug in your software, you don’t want to rush a fix that might introduce more problems. You create a special branch just for fixing that specific bug, so you can test it thoroughly before putting it into production.
  3. Experimentation and Testing
    Sometimes developers want to try out new approaches or technologies. They create experimental branches where they can explore without affecting the main stable code that other people depend on.

The Main Branch (also called Master)

The main branch is like the official version of your project – the one that’s considered “ready” and working properly. It represents what should be in production, what users are actually using, and what’s being deployed to customers.

This branch is typically very stable and only gets updated when features have been thoroughly tested and approved.

Understanding HEAD

HEAD is like a marker that always points to your current location in the project history. It tells Git: “Hey, I’m currently working on this specific point in time.”

Think of HEAD as a spotlight that always shines on where you are right now:

When you’re working on the main branch, HEAD points to the latest commit on main
When you switch to a feature branch, HEAD moves to that branch’s latest commit
HEAD is always pointing to exactly one commit – it never points to multiple places

How Branches Work Together

When you create a branch from the main codebase, you’re essentially making a copy of everything that existed at that moment. From there, you can make unlimited changes without affecting the original.

Eventually, when your work is ready, you can merge those changes back into the main branch – kind of like bringing your experimental work back to the official project.

Full Working Git Example

Let me walk you through a complete, step-by-step Git workflow with actual commands:

Step 1: Initialize the Repository
Create a new project directory
and Initialize Git repository

mkdir restaurant-website
cd restaurant-website

git init

Step 2: Set up Initial Project Structure
Create basic files for our restaurant website
and Add and commit the initial files

echo "# Restaurant Website" > README.md
echo "<html><body><h1>Welcome to Our Restaurant</h1></body></html>" > index.html

git add .
git commit -m "Initial commit: Basic website structure"

Step 3: Create a Feature Branch
Create a new branch for our menu feature
Now we’re on the feature branch (HEAD points to this branch)
Make changes to add menu functionality
Add and commit the changes in the feature branch
git add index.html

git checkout -b feature/menu

echo "<h2>Our Menu</h2><ul><li>Burger</li><li>Pizza</li></ul>" >> index.html

git commit -m "Add basic menu items"

Step 4: Continue Working on Feature
Make more changes to our menu and Commit these changes

echo "<h3>Appetizers</h3><ul><li>Caesar Salad</li><li>Fried Calamari</li></ul>" >> index.html
echo "<h3>Main Courses</h3><ul><li>Grilled Salmon</li><li>Beef Steak</li></ul>" >> index.html

git add index.html
git commit -m "Add menu categories and items"

Step 5: Work on Another Feature
Switch back to main branch to start working on something else, Create another feature branch for reservations, Add reservation functionality and Commit this change

git checkout main

git checkout -b feature/reservations

echo "<h2>Make a Reservation</h2><form><input type='text' placeholder='Name'></form>" >> index.html

git add index.html
git commit -m "Add reservation form"

Step 6: Return to Menu Feature
Switch back to menu branch to continue working, Add more menu items and Commit the additional changes

git checkout feature/menu

echo "<h3>Desserts</h3><ul><li>Chocolate Cake</li><li>Tiramisu</li></ul>" >> index.html

git add index.html
git commit -m "Add dessert section to menu"

Step 7: Test and Merge Menu Feature
Switch back to main branch, Check what branches exist and Merge our completed menu feature into main. The main branch now includes all our menu changes. View the updated main branch

git checkout main

git branch -a

git merge feature/menu

git log --oneline

Step 8: Continue Working on Reservations
Switch back to reservations branch. Add more reservation functionality and Commit this change. Then Merge reservations into main

git checkout feature/reservations

echo "<p>Available times: 5pm, 6pm, 7pm, 8pm</p>" >> index.html

git add index.html
git commit -m "Add reservation time slots"

git checkout main
git merge feature/reservations

Step 9: View Final Result
See the complete history
Check what our final website looks like

git log --oneline --graph --all

cat index.html

Step 10: Create a Bug Fix Branch
Discover there’s a bug in our menu – missing closing tags. Fix the HTML structure. Commit the fix and Merge the bug fix back to main

git checkout -b fix/menu-bug

git add index.html
git commit -m "Fix HTML structure in menu"

git checkout main
git merge fix/menu-bug

Git Branching Questions

  1. What happens to your local branches when you clone a remote repository?
  2. How does Git handle merging when two branches have made changes to the same file in different ways?
  3. Why would you want to use git rebase instead of git merge in certain situations?
  4. What is the difference between a branch and a tag in Git?
  5. How does Git’s branching system make collaboration easier for teams working on the same project?
  6. What happens to unmerged branches when you delete a remote branch?
  7. Why might you choose to use feature branches instead of working directly on the main branch?
  8. How does Git determine which commits belong to which branch?

Leave a Reply