Fatskills
Practice. Master. Repeat.
Study Guide: Git and GitHub Branching Essentials - Zero-Fluff, Hands-On Guide
Source: https://www.fatskills.com/web-development/chapter/tech-git-github-branching-essentials-zero-fluff-hands-on-guide

Git and GitHub Branching Essentials - Zero-Fluff, Hands-On Guide

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~8 min read

Git & GitHub Branching Essentials: Zero-Fluff, Hands-On Guide

(For engineers who need to ship code, not just pass exams)


1. What This Is & Why It Matters

Branching is Git’s superpower. It lets you: - Work on features, fixes, or experiments without breaking production. - Collaborate with a team without stepping on each other’s changes. - Test ideas safely before merging them into the main codebase.

Real-world scenario: You’re a backend engineer at a SaaS company. The main branch is deployed to production. Your PM drops a last-minute request: "We need a dark mode toggle by EOD." If you commit directly to main, you risk: - Breaking the live app. - Blocking other engineers from deploying their work. - Losing track of what changed (no audit trail).

Branching solves this. You create a feature/dark-mode branch, implement the change, test it, and merge it only when ready. If the PM changes their mind, you just delete the branch—no harm done.

What breaks if you ignore branching? - Production outages (unvetted code in main). - Merge conflicts (multiple people editing the same files). - Chaos in CI/CD (pipelines running on half-finished work). - Lost work (no way to isolate experiments).


2. Core Concepts & Components

1. git branch

  • Definition: Lists, creates, or deletes branches.
  • Production insight: Always check which branch you’re on (git branch --show-current) before committing. Committing to the wrong branch is a top cause of "oops, I broke prod" incidents.

2. git checkout -b

  • Definition: Creates a new branch and switches to it in one command.
  • Production insight: Use this when starting a new feature/bugfix. Example: git checkout -b feature/user-auth creates and checks out a branch for authentication work.

3. git switch (Git 2.23+)

  • Definition: A safer, more intuitive alternative to git checkout for switching branches.
  • Production insight: Prefer git switch over git checkout for branch navigation—it’s less error-prone (e.g., git switch feature/dark-mode).

4. Local vs. Remote Branches

  • Definition:
  • Local branch: Exists only on your machine (e.g., feature/dark-mode).
  • Remote branch: Exists on GitHub/GitLab (e.g., origin/feature/dark-mode).
  • Production insight: Always fetch (git fetch) before creating a branch to avoid conflicts with remote changes.

5. Branch Naming Conventions

  • Definition: A consistent way to name branches (e.g., feature/, bugfix/, hotfix/).
  • Production insight: Enforce naming rules in your team’s CONTRIBUTING.md. Example:
  • feature/add-login (new functionality)
  • bugfix/fix-login-error (bug fix)
  • hotfix/db-connection-leak (urgent production fix)

6. HEAD

  • Definition: A pointer to the current branch/commit you’re working on.
  • Production insight: If HEAD is "detached" (e.g., after git checkout <commit-hash>), you’re not on a branch. Never commit in detached HEAD state—your changes will be lost.

7. git merge vs. git rebase

  • Definition:
  • Merge: Combines branches by creating a new "merge commit."
  • Rebase: Rewrites commit history to make it linear.
  • Production insight:
  • Use merge for public branches (e.g., main) to preserve history.
  • Use rebase for local branches to clean up commits before merging.

8. git push --set-upstream (or -u)

  • Definition: Links a local branch to a remote branch for future git push/git pull without specifying the remote.
  • Production insight: Always set upstream when pushing a new branch (git push -u origin feature/dark-mode). Otherwise, git push will fail.

3. Step-by-Step Hands-On: Branching Workflow

Prerequisites

  • A Git repo (local or cloned from GitHub).
  • Basic Git knowledge (e.g., git add, git commit, git push).

Task: Implement a Feature Without Breaking Production

You’re working on a web app. The main branch is deployed to production. Your task: Add a "Forgot Password" link to the login page.

Step 1: Check Your Current Branch

git branch --show-current  # Should output "main" or "master"
git status                # Ensure no uncommitted changes

Step 2: Create and Switch to a Feature Branch

git checkout -b feature/forgot-password
# OR (Git 2.23+)
git switch -c feature/forgot-password

Verify:

git branch  # Should show "* feature/forgot-password"

Step 3: Make Your Changes

Edit login.html to add the "Forgot Password?" link:

<a href="/reset-password">Forgot Password?</a>

Save the file.

Step 4: Commit Your Changes

git add login.html
git commit -m "feat: add forgot password link to login page"

Step 5: Push the Branch to GitHub

git push -u origin feature/forgot-password

Verify on GitHub: - Go to your repo on GitHub. - You should see a new branch feature/forgot-password with your commit.

Step 6: Create a Pull Request (PR)

  1. On GitHub, click "Compare & pull request" for your branch.
  2. Add a description (e.g., "Adds a 'Forgot Password?' link to the login page").
  3. Assign a reviewer (e.g., your teammate).
  4. Click "Create pull request".

Step 7: Merge the PR (After Approval)

  1. Click "Merge pull request" on GitHub.
  2. Delete the branch (optional but recommended): bash git branch -d feature/forgot-password # Delete local branch git push origin --delete feature/forgot-password # Delete remote branch

Step 8: Update Your Local main Branch

git checkout main
git pull origin main

Verify:

git log -1  # Should show your new commit in main

4.-Production-Ready Best Practices

Security

  • Protect main/master: Use GitHub’s "Branch protection rules" to:
  • Require PR reviews before merging.
  • Require status checks (e.g., CI tests) to pass.
  • Prevent force-pushing.
  • Never commit secrets: Use git-secrets or GitHub’s secret scanning to block API keys, passwords, etc.

Cost Optimization

  • Delete stale branches: Old branches clutter the repo and slow down git fetch. Run: bash git fetch --prune # Removes deleted remote branches from local git branch --merged | grep -v "main" | xargs git branch -d # Delete merged local branches

Reliability & Maintainability

  • Use descriptive branch names: feature/add-user-profile > fix-bug.
  • Rebase before merging: Keeps history clean: bash git checkout feature/forgot-password git rebase main # Rebase onto latest main git push --force-with-lease # Update remote branch
  • Squash merge PRs: For small features, use GitHub’s "Squash and merge" to combine commits into one.

Observability

  • Monitor branch age: Use GitHub’s "Insights > Network" to spot stale branches.
  • Enforce commit message conventions: Use commitlint to require messages like: feat: add forgot password link fix: correct login error

5. Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Committing to main directly Production breaks, team yells at you. Always work in a branch. Use branch protection rules to block direct pushes.
Not pulling latest main before branching Merge conflicts later. Run git pull origin main before creating a branch.
Forgetting to set upstream (-u) git push fails with "no upstream branch." Always use git push -u origin <branch> for new branches.
Detached HEAD state Commits disappear after switching branches. Never commit in detached HEAD. Use git switch <branch> to return to a branch.
Force-pushing to shared branches Teammates lose work. Use --force-with-lease instead of --force. Communicate before force-pushing.

6.-Exam/Certification Focus

Typical Question Patterns

  1. "Which command creates and switches to a new branch?"
  2. ? git checkout -b <branch> or git switch -c <branch>
  3. git branch <branch> (only creates, doesn’t switch)

  4. "How do you delete a local branch?"

  5. ? git branch -d <branch> (safe, checks for unmerged changes)
  6. ? git branch -D <branch> (force delete)
  7. git delete <branch> (not a real command)

  8. "What’s the difference between git fetch and git pull?"

  9. git fetch: Downloads changes from remote but doesn’t merge.
  10. git pull: Fetches and merges (git pull = git fetch + git merge).

  11. "You’re on feature/x and want to update it with latest main. What do you do?"

  12. ? git rebase main (clean history)
  13. ? git merge main (creates a merge commit)

Key Trap Distinctions

  • git checkout vs. git switch:
  • git checkout can switch branches or restore files (confusing).
  • git switch only switches branches (safer).
  • git branch -d vs. git branch -D:
  • -d: Safe delete (checks for unmerged changes).
  • -D: Force delete (use when you’re sure).
  • git pull vs. git pull --rebase:
  • git pull: Merges remote changes (creates a merge commit).
  • git pull --rebase: Rebases local changes on top of remote (cleaner history).

7.-Hands-On Challenge

Challenge: Fix a Bug Without Breaking Production

Scenario: You’re working on a team project. The main branch is deployed to production. A bug is reported: "The login button is misaligned on mobile." Your task:
1. Create a branch to fix the bug.
2. Make the fix (edit styles.css to center the button).
3. Commit and push the branch.
4. Create a PR and merge it after approval.

Solution:

git checkout main
git pull origin main
git checkout -b bugfix/fix-login-button
# Edit styles.css to center the button
git add styles.css
git commit -m "fix: center login button on mobile"
git push -u origin bugfix/fix-login-button
# Create PR on GitHub, get approval, merge
git checkout main
git pull origin main
git branch -d bugfix/fix-login-button

Why it works: - You isolated the fix in a branch, preventing production issues. - You pulled latest main to avoid conflicts. - You cleaned up the branch after merging.


8.-Rapid-Reference Crib Sheet

Command Description Example
git branch List local branches. git branch
git branch -a List all branches (local + remote). git branch -a
git branch <name> Create a branch (doesn’t switch). git branch feature/x
git checkout -b <name> Create and switch to a branch. git checkout -b feature/x
git switch -c <name> Create and switch to a branch (Git 2.23+). git switch -c feature/x
git switch <branch> Switch to an existing branch. git switch main
git branch -d <branch> Delete a local branch (safe). git branch -d feature/x
git branch -D <branch> Force-delete a local branch. git branch -D feature/x
git push -u origin <branch> Push a branch to remote and set upstream. git push -u origin feature/x
git push origin --delete <branch> Delete a remote branch. git push origin --delete feature/x
git fetch --prune Remove deleted remote branches from local. git fetch --prune
git rebase <branch> Rebase current branch onto <branch>. git rebase main
git push --force Dangerous! Overwrites remote history. Use --force-with-lease instead. git push --force-with-lease

9.-Where to Go Next

  1. GitHub Docs: Branching – Official guide to branching workflows.
  2. Git Branching Tutorial (Atlassian) – Deep dive into branching strategies.
  3. Git Flight Rules – What to do when things go wrong (e.g., "I accidentally committed to main").
  4. Learn Git Branching (Interactive) – Hands-on game to master branching.