Skip to main content

Command Palette

Search for a command to run...

Git Branching in Practice: Build a Client App with Variations

Updated
β€’6 min read
Git Branching in Practice: Build a Client App with Variations
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

In this tutorial, you'll learn how to implement Git branching effectively by managing specific features of a multi-page PHP application. This approach allows you to work on individual pages without affecting the entire project.

🎯 What You’ll Build

You will create a 3-page PHP app with the following structure:

  • index.php β€” Front Page (Home)

  • clients.php β€” Client List (2 variations β†’ 2 branches)

  • client-details.php β€” Client Details (3 variations β†’ 3 branches)

You’ll Learn:

  • βœ… How to structure a multi-page project

  • βœ… How to create branches for specific files/pages

  • βœ… How to switch between variations of a single page while keeping others stable

  • βœ… How to avoid conflicts and manage scope

🧭 Step-by-Step Tutorial (Replit + PHP + Git)

βœ… STEP 0: Create Your Replit Project

  1. Go to Replit: Visit Replit and sign in or sign up.

  2. Create PHP Web Project:

    • Click + Create.

    • Search for β€œPHP Web”.

    • Select the β€œPHP Web Server” template.

    • Name it client-app-variations.

    • Click Create Repl.

βœ… STEP 1: Initialize Git & Create Base Structure

  1. Open Shell & Initialize Git:

    bash

     git init
    
  2. Configure Git (One Time):

    bash

     git config --global user.name "YourName"
     git config --global user.email "you@example.com"
    
  3. Create Base Files:

    • Create index.php:

php

    <!DOCTYPE html>
    <html>
    <head><title>Client App β€” Home</title></head>
    <body>
        <h1>🏠 Client Management App</h1>
        <p><a href="clients.php">β†’ View Client List</a></p>
    </body>
    </html>
  • Create clients.php:

php

    <!DOCTYPE html>
    <html>
    <head><title>Client List β€” Base</title></head>
    <body>
        <h1>πŸ“‹ Client List (Base Version)</h1>
        <ul>
            <li><a href="client-details.php?id=1">Alex Corp</a></li>
            <li><a href="client-details.php?id=2">Beta LLC</a></li>
            <li><a href="client-details.php?id=3">Gamma Inc</a></li>
        </ul>
        <p><a href="index.php">← Back Home</a></p>
    </body>
    </html>
  • Create client-details.php:

php

    <!DOCTYPE html>
    <html>
    <head><title>Client Details β€” Base</title></head>
    <body>
        <h1>πŸ“„ Client Details (Base Version)</h1>
        <?php
        $id = $_GET['id'] ?? 'unknown';
        echo "<p>Client ID: $id</p>";
        ?>
        <p><a href="clients.php">← Back to List</a></p>
    </body>
    </html>
  1. Add & Commit All Files:

    bash

     git add .
     git commit -m "Initial commit: Base 3-page app structure"
    

βœ… STEP 2: Create 2 Variations of clients.php (Client List)

We’ll create 2 branches, each modifying only clients.php.

  1. Create Branch: feature/list-table-view:

    bash

     git checkout -b feature/list-table-view
    
  2. Edit clients.php β€” Table Layout:

    php

     <!DOCTYPE html>
     <html>
     <head><title>Client List β€” Table View</title></head>
     <body>
         <h1>πŸ“‹ Client List (Table View)</h1>
         <table border="1" cellpadding="10">
             <tr><th>Client Name</th><th>Action</th></tr>
             <tr><td>Alex Corp</td><td><a href="client-details.php?id=1">View</a></td></tr>
             <tr><td>Beta LLC</td><td><a href="client-details.php?id=2">View</a></td></tr>
             <tr><td>Gamma Inc</td><td><a href="client-details.php?id=3">View</a></td></tr>
         </table>
         <p><a href="index.php">← Back Home</a></p>
     </body>
     </html>
    
  3. Commit Changes:

    bash

     git add clients.php
     git commit -m "feat: list as table layout"
    
  4. Create Branch: feature/list-cards-view:

    bash

     git checkout main
     git checkout -b feature/list-cards-view
    
  5. Edit clients.php β€” Card Layout:

    php

     <!DOCTYPE html>
     <html>
     <head><title>Client List β€” Cards View</title></head>
     <body>
         <h1>πŸ“‹ Client List (Cards View)</h1>
         <div style="display: flex; gap: 20px; flex-wrap: wrap;">
             <div style="border: 1px solid #ccc; padding: 15px; width: 200px;">
                 <h3>Alex Corp</h3>
                 <a href="client-details.php?id=1">View Details</a>
             </div>
             <div style="border: 1px solid #ccc; padding: 15px; width: 200px;">
                 <h3>Beta LLC</h3>
                 <a href="client-details.php?id=2">View Details</a>
             </div>
             <div style="border: 1px solid #ccc; padding: 15px; width: 200px;">
                 <h3>Gamma Inc</h3>
                 <a href="client-details.php?id=3">View Details</a>
             </div>
         </div>
         <p><a href="index.php">← Back Home</a></p>
     </body>
     </html>
    
  6. Commit Changes:

    bash

     git add clients.php
     git commit -m "feat: list as card layout"
    

βœ… STEP 3: Create 3 Variations of client-details.php

Each branch modifies only client-details.php.

  1. Go Back to Main:

    bash

     git checkout main
    
  2. Create Branch: feature/details-expanded:

    bash

     git checkout -b feature/details-expanded
    
  3. Edit client-details.php β€” Expanded View:

    php

     <!DOCTYPE html>
     <html>
     <head><title>Client Details β€” Expanded</title></head>
     <body>
         <h1>πŸ“„ Client Details (Expanded View)</h1>
         <?php
         $id = $_GET['id'] ?? 'unknown';
         $client = [
             1 => ['name' => 'Alex Corp', 'email' => 'alex@corp.com', 'phone' => '+1-555-0101'],
             2 => ['name' => 'Beta LLC', 'email' => 'beta@llc.com', 'phone' => '+1-555-0102'],
             3 => ['name' => 'Gamma Inc', 'email' => 'gamma@inc.com', 'phone' => '+1-555-0103']
         ];
         if (isset($client[$id])) {
             echo "<h2>{$client[$id]['name']}</h2>";
             echo "<p><strong>Email:</strong> {$client[$id]['email']}</p>";
             echo "<p><strong>Phone:</strong> {$client[$id]['phone']}</p>";
         } else {
             echo "<p>Client not found.</p>";
         }
         ?>
         <p><a href="clients.php">← Back to List</a></p>
     </body>
     </html>
    
  4. Commit Changes:

    bash

     git add client-details.php
     git commit -m "feat: details with expanded contact info"
    
  5. Create Branch: feature/details-minimal:

    bash

     git checkout main
     git checkout -b feature/details-minimal
    
  6. Edit client-details.php β€” Minimal View:

    php

     <!DOCTYPE html>
     <html>
     <head><title>Client Details β€” Minimal</title></head>
     <body>
         <h1>πŸ“„ Client Details (Minimal)</h1>
         <?php
         $id = $_GET['id'] ?? 'unknown';
         echo "<p>Client #$id</p>";
         ?>
         <p><a href="clients.php">← Back</a></p>
     </body>
     </html>
    
  7. Commit Changes:

    bash

     git add client-details.php
     git commit -m "feat: minimal details view"
    
  8. Create Branch: feature/details-interactive:

    bash

     git checkout main
     git checkout -b feature/details-interactive
    
  9. Edit client-details.php β€” Interactive View:

    php

     <!DOCTYPE html>
     <html>
     <head><title>Client Details β€” Interactive</title></head>
     <body>
         <h1>πŸ“„ Client Details (Interactive)</h1>
         <?php
         $id = $_GET['id'] ?? 'unknown';
         echo "<p>Client ID: <span id='client-id'>$id</span></p>";
         ?>
         <button onclick="copyId()">πŸ“‹ Copy ID</button>
         <p id="status"></p>
         <script>
             function copyId() {
                 const id = document.getElementById('client-id').textContent;
                 navigator.clipboard.writeText(id).then(() => {
                     document.getElementById('status').textContent = "Copied!";
                     setTimeout(() => document.getElementById('status').textContent = "", 2000);
                 });
             }
         </script>
         <p><a href="clients.php">← Back to List</a></p>
     </body>
     </html>
    
  10. Commit Changes:

    bash

    git add client-details.php
    git commit -m "feat: interactive details with copy button"
    

βœ… STEP 4: Practice Switching β€” Mix & Match Variations

You can now combine any list variation with any details variation since they’re in separate branches.

  1. Example: Table List + Expanded Details:

    bash

     git checkout feature/list-table-view
     git checkout feature/details-expanded -- client-details.php
    

    Refresh your browser to see the table list with expanded details!

  2. Example: Cards List + Interactive Details:

    bash

     git checkout feature/list-cards-view
     git checkout feature/details-interactive -- client-details.php
    

    Refresh to see cards with the copy button!

  3. Example: Base List + Minimal Details:

    bash

     git checkout main
     git checkout feature/details-minimal -- client-details.php
    

    You’re on the main (base list) but using the minimal details view.

βœ… STEP 5: Clean Checkout β€” Reset to Pure Branch

If you want to return to a pure branch without mixed files:

  1. Reset Specific File:

    bash

     git checkout feature/list-table-view
     git checkout HEAD -- client-details.php
    
  2. Reset Everything:

    bash

     git checkout feature/list-table-view
     git reset --hard
    

    ⚠️ Note: reset --hard discards all uncommitted changes. Use with caution.

πŸ”„ Mistake Recovery Guide

  • Edited wrong file:

    bash

      git checkout -- <filename>
    
  • Mixed branches and confused:

    bash

      git status
      git checkout <branch> -- <file>
    
  • Want to start clean:

    bash

      git checkout <branch>
      git reset --hard
    
  • Accidentally committed wrong file:

    bash

      git reset HEAD~1
    
  • Can’t switch branch due to changes:

    bash

      git stash push -m "WIP"
      git stash apply
    

πŸŽ“ Key Points

  • βœ… Branch by feature/page, not the whole app

  • βœ… Partial checkout for mixing branches safely

  • βœ… Isolation of changes in one file

  • βœ… Flexibility to combine variations

Final Project Structure

dsconfig

client-app-variations/
β”œβ”€β”€ index.php                  ← Always stable (no branches)
β”œβ”€β”€ clients.php                ← Modified in: list-table-view, list-cards-view
β”œβ”€β”€ client-details.php         ← Modified in: details-expanded, details-minimal, details-interactive
└── .git/                      ← Tracks all branches and variations

πŸŽ‰ Congratulations! You’ve successfully practiced real-world Git branching, focusing on targeted, modular, page-level experimentation.

2 views