Why Branch? The Power of Parallel Development

Imagine your main codebase is a tree trunk (the main branch). This trunk should always be stable and ready for deployment. Now, what if you want to add a new feature, like a user login system? If you work directly on the main trunk, you risk introducing bugs that could break the entire application for everyone.

Instead, you create a branch. A branch is essentially a copy of your project at a certain point in time that you can work on in isolation. It's like a separate branch growing off the main trunk. You can build your new feature on this branch without any risk to the stable `main` branch. Once your feature is complete and tested, you then merge it back into the main trunk.

This workflow is the cornerstone of modern software development, allowing teams to work on dozens of features simultaneously without interfering with each other.


The Basic Branching Workflow

The workflow is a simple cycle:

  1. Create a new branch from main.
  2. Switch to your new branch.
  3. Do your work: add files, modify files, make commits.
  4. When you're finished, switch back to the main branch.
  5. Merge your new branch into main.
  6. (Optional but recommended) Delete your feature branch.

Your Mission: Creating Your First Feature Branch

Let's practice this workflow by pretending to build a "user profile" feature in our mvc-project.

Step 1: Create and Switch to a New Branch

Open your terminal in the mvc-project directory. You can create a branch and switch to it in one convenient command:

git checkout -b feature/user-profile
  • checkout is the command for switching branches.
  • The -b flag tells Git to "create a new branch" before switching.
  • feature/user-profile is a common naming convention for branches. It indicates this is a 'feature' branch and describes what it's for.

Your terminal will now say "Switched to a new branch 'feature/user-profile'". Any commits you make from now on will be on this new branch only.

Step 2: Do Some "Work" on the Branch

Let's simulate adding the new feature by creating a placeholder controller file.

Create a new file: app/Controllers/UserController.php


<?php

namespace App\Controllers;

class UserController
{
    public function show($id)
    {
        // Logic to show a user profile will go here.
        echo "Displaying profile for user ID: " . htmlspecialchars($id);
    }
}

Now, let's commit this new file to our feature branch.

# Stage the new file
git add app/Controllers/UserController.php

# Commit the change
git commit -m "feat: Add UserController for user profiles"

(Note: "feat:" is a common prefix for commit messages that add a new feature.)

Step 3: Switch Back to `main`

Our feature is "complete." Let's go back to our main branch.

git checkout main

Now, look inside your app/Controllers/ directory. The UserController.php file has disappeared! Don't worry, this is normal. The file only exists on the feature/user-profile branch. The main branch doesn't have it yet.

Step 4: Merge the Feature into `main`

This is the magic moment. While on the `main` branch, run the following command to merge the changes from your feature branch.

git merge feature/user-profile

Git will perform a "fast-forward" merge, applying the commit(s) from your feature branch onto `main`. Now, check your app/Controllers/ directory again. The UserController.php file is back!

Step 5: Clean Up (Delete the Branch)

Since the feature has been successfully merged, we no longer need the branch. It's good practice to delete it to keep your repository clean.

git branch -d feature/user-profile

Conclusion

You've now learned the complete, fundamental workflow of Git: creating branches to work in isolation and merging them back when the work is done. This skill is absolutely essential for team collaboration and for managing projects of any size. In the next lesson, we'll see how to use this workflow with a remote repository like GitHub.