Coding/Learn/git/git branching and merging
Git & GitHub Complete Course
01.Introduction, Architecture & Configuration
02.Staging Area, atomic Commits & git status
03.Branching, Fast-Forward & 3-Way Merging
04.Stash, Log Visualization & History Inspection
05.Workflows (Git Flow, Trunk-Based) & Semantic Tagging
06.SSH Keys, Remote Repositories & Origin Setup
07.Push, Fetch, Pull & Upstream Tracking Branches
08.Forking, Clones & Open-Source Pull Requests (PRs)
09.GitHub Flow, Branch Protection & GitHub Pages
10.Undoing Changes: git restore & Safe Public git revert
11.Reset: Deep Dive into --soft, --mixed & --hard
12.Commit Amending & Interactive Rebase (git rebase -i)
13.Reflog & Disaster Recovery (Reviving "Lost" Commits)
14.Merge Conflicts Resolution & git rerere
15..gitignore Rules & .gitattributes Cross-Platform Handling
16.Hooks & Automated Quality Assurance (Pre-Commit)
17.Submodules & Large File Storage (Git LFS)
18.GPG Cryptographic Signing & git cherry-pick
19.Advanced Remotes, Garbage Collection (git gc) & Maintenance
20.Capstone: Production Multi-Branch Enterprise Release Pipeline
Course Certificate0%

Complete all lessons to earn

LearnBranching, Fast-Forward & 3-Way Merging
3 / 20 0%
Lesson 03 of 20
6 mins read

Branching, Fast-Forward & 3-Way Merging

PreviousNext Lesson
In Plain English

A branch in Git is simply a lightweight 41-byte movable pointer referencing a commit SHA. Switching branches (git switch / git checkout) updates HEAD and the working tree. Merging integrates divergent branch histories via Fast-Forward or 3-Way merge commits.

The Mental Model

"A branch is a bookmark in a book: creating a branch does not duplicate the book, it simply places a new sticky note on the current page."

Deep Dive: How It Works

HEAD Reference: A special pointer indicating which local branch is currently checked out.

Fast-Forward Merge: If target branch has no divergent commits, Git simply slides the pointer forward.

3-Way Merge: If branches diverged, Git finds the common ancestor and creates a new merge commit with 2 parents.

Syntax Blueprint

git switch -c feature/auth
# Work & commit
git switch main
git merge feature/auth

Create and switch to feature branch, commit changes, return to main, and merge.

PointCore Rules to Remember

Point star
Zero Overhead Branches: Creating a branch is instantaneous because Git only writes a 40-character SHA file.
Point star
git switch vs checkout: git switch (introduced in Git 2.23) separates branch switching from file restoring.
Point star
Squash Merging: git merge --squash combines all feature branch commits into a single clean commit on main.

Common Beginner Traps & How to Fix Them

Mistake:Using git checkout for both branch switching and file restoration, causing accidental file overwrites.

Why it happens: Legacy checkout had overloaded responsibilities.

How to fix: Use git switch for branches and git restore for discarding working directory changes.

Pro Tip: Delete merged feature branches immediately with git branch -d <name> to keep repository navigation clean.

Live Interactive Example

Hit Run Code to see it live
Feature Branch Lifecycle
Git 2.45 / CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: Fast-forward merge updates main pointer directly to the commit created on feature/v1.1.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create and Merge Feature Branch

  • PointCreate and switch to a new branch named "feature/metrics".
  • PointAdd a file metrics.log with content "cpu: 40%".
  • PointStage and commit with message "feat: add metrics tracking".
  • PointSwitch back to main and merge feature/metrics.
1
2
3
4
5
6
7
8
9
10
11
12
13
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates