Skip to main content

Git Branches & Merging Basics

When multiple developers work on the same project (or when you are testing a risky new feature), you should never edit the stable main branch directly.

Git Branches let you diverge from the main line of development to write and test code in complete isolation.


1. Beginner Mental Model: The Parallel Universe

main branch (Production App) ──●───●──────────────────● (Merge!)
\ /
feature-payment branch └───●─────●────┘
  • main: The stable, working version of your app running on live servers.
  • feature-payment branch: A temporary parallel universe. You can experiment, make mistakes, and test without breaking the live app for users.
  • Merge: Once the feature is tested and working, you safely combine it back into main.

2. Core Branching Commands

Step 1: Create and switch to a new branch

# Creates and switches to a branch named 'feature-auth' in 1 command:
git checkout -b feature-auth

(In modern Git, you can also use git switch -c feature-auth).


Step 2: Work and commit your feature

git add .
git commit -m "Add user login and token generation"

(These commits exist strictly on your feature-auth branch! main remains untouched).


Step 3: Switch back to main and merge

# 1. Switch back to the main branch:
git checkout main

# 2. Merge the feature branch into main:
git merge feature-auth

Step 4: Delete the completed branch

git branch -d feature-auth

3. What is a Pull Request (PR) on GitHub?

In professional tech companies, developers never merge directly on their local computers.

Instead, you push your feature branch to GitHub and open a Pull Request (PR). A senior engineer reviews your code line-by-line, leaves feedback, and clicks the "Merge PR" button on GitHub when approved!


Quick Summary

  • Branching Purpose: Isolates experimental code or new features from the stable main production branch.
  • Key Commands: git checkout -b branch_name (create and switch), git checkout main (switch), git merge branch_name (integrate changes).
  • Pull Requests (PRs): Cloud-based review workflows on GitHub where team members inspect diffs and run CI tests before merging code into main.

What's Next?

Now that you have mastered version control, let's explore high-performance functional programming patterns and generators in Module 18: Advanced Functions!