Skip to main content

Command Palette

Search for a command to run...

Git Branching Cheatsheet

Updated
4 min read
Git Branching Cheatsheet
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).

Introduction

Learn to use Git's powerful collaboration tools—branching, stashing, tagging, and merging—by building and switching between three versions of a demo app. No setup required; just your browser!


Quick Start Guide

Step 0: Create Your Replit Project

  1. Sign in to [Replit].

  2. Create a New Project:

    • Select PHP Web Server Framework.

    • Name it GitQuizDemo.


Step 1: Initialize Git & Create Base App

  1. Check Git Version: bash

     git --version
    
  2. Configure Git: bash

     git config --global user.name "YourName"
     git config --global user.email "youremail@example.com"
     git config --global init.defaultBranch main
    
  3. Initialize Git: bash

     git init
    
  4. Check Configuration: bash

     git config --global --list
    

Step 2: Edit Index Page

  • Open index.php and paste the following:

html

<!DOCTYPE html>
<html>
<head>
    <title>Demo App — Base</title>
</head>
<body>
    <h1>🚀 Welcome to Demo App!</h1>
    <p>Switch branches to see different versions.</p>
</body>
</html>

html 1

Open on canvas

Step 3: Commit — First Snapshot of Main

bash

git add index.php
git commit -m "Initial commit: Base version"

Step 4: Practice Branching — Create Dark Theme

  1. Create and Switch to Dark Theme Branch: bash

     git checkout -b feature/dark-theme
    
  2. Edit index.php for Dark Theme:

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyKahoot — Dark Theme</title>
    <style>
        body { background: #121212; color: #e0e0e0; }
        h1 { color: #bb86fc; }
    </style>
</head>
<body>
    <h1>Quiz Night</h1>
    <p>Welcome to the ultimate quiz game!</p>
</body>
</html>

html 2

Open on canvas

  1. Commit Changes:

bash

git add index.php
git commit -m "feat: dark theme UI"

Step 5: Edit Index Page Again

  • Undo Changes:

bash

git checkout -- index.php

Step 6: Create Timer Version from Main

  1. Switch to Main: bash

     git checkout main
    
  2. Create Timer Branch: bash

     git checkout -b feature/timer
    
  3. Edit index.php for Timer:

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MyKahoot — Timer Quiz</title>
</head>
<body>
    <h1>Timer Active</h1>
    <p>Next question in <span id="countdown">10</span> seconds!</p>
    <script>
        let timeLeft = 10;
        const timer = setInterval(() => {
            document.getElementById('countdown').textContent = --timeLeft;
            if (timeLeft <= 0) clearInterval(timer);
        }, 1000);
    </script>
</body>
</html>

html 3

Open on canvas

  1. Commit Changes:

bash

git add index.php
git commit -m "feat: added countdown timer"

Step 7: Create Leaderboard Version

  1. Switch to Main: bash

     git checkout main
    
  2. Create Leaderboard Branch: bash

     git checkout -b feature/leaderboard
    
  3. Edit index.php for Leaderboard:

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DemoApp — Leaderboard</title>
</head>
<body>
    <h1>Top Players</h1>
    <ol>
        <li>Alex — 9500 pts</li>
        <li>Jamie — 8700 pts</li>
        <li>Taylor — 8200 pts</li>
    </ol>
</body>
</html>

html 4

Open on canvas

  1. Commit Changes:

bash

git add index.php
git commit -m "feat: added leaderboard"

Step 8: Switch Between All Branches

  • Switch and Refresh Browser:

bash

git checkout feature/dark-theme
git checkout feature/timer
git checkout feature/leaderboard
git checkout main

Step 9: Practice Stashing

  1. Edit index.php:

html

<p>🚧 COMING SOON: Sound Effects!</p>
  1. Stash Changes:

bash

git stash push -m "WIP: sound effects teaser"
  1. Switch Branch:

bash

git checkout feature/dark-theme
  1. Retrieve Stash:

bash

git checkout main
git stash apply

Step 10: Practice Tagging

  1. Tag the Dark Theme:

bash

git checkout feature/dark-theme
git tag -a v1.0 -m "First release: Dark Theme Edition"
  1. List Tags:

bash

git tag -n
  1. Switch to Tag:

bash

git checkout v1.0

Step 11: Practice Merging (Optional)

  1. Merge Dark Theme into Main:

bash

git checkout main
git merge feature/dark-theme
  1. Resolve Merge Conflicts (if any):
  • Edit the file to resolve conflicts, then:

bash

git add index.php
git commit -m "Resolved merge conflict"

Key Commands Summary

CommandDescription
git --versionCheck Git version
git config --global user.name "YourName"Set username
git initInitialize a Git repository
git checkout -b <branch>Create and switch to a new branch
git add <file>Stage changes for commit
git commit -m "message"Commit staged changes with a message
git branchList all branches
git stash push -m "message"Stash uncommitted changes
git tag -a <tag> -m "message"Create a tag
git merge <branch>Merge a branch into the current branch

Conclusion

You've mastered Git branching techniques! You can now create isolated features, stash unfinished work, tag releases, and merge changes effectively. Happy coding!

2 views