3.5 Hours

3. Branching: Creating, Switching, and Merging

Branching

Branching is Git's killer feature. Branches allow you to work on multiple features simultaneously without interfering with each other. They're cheap, lightweight, and fast. Understanding branching is essential for effective Git usage. Branching is what makes Git different from previous version control systems — in SVN, branching was expensive and slow. In Git, branches are just pointers, making them fast and easy.

The key insight is that a branch is just a pointer to a commit. Creating a branch simply creates a new pointer. Switching branches moves the HEAD pointer. This is why branches are so lightweight — they're just references. This makes branching a core part of Git's workflow. You can create a branch in a fraction of a second, experiment freely, and merge it back when ready. This is why Git enables workflows like GitHub Flow and Git Flow.

This module covers creating branches, switching between them, merging branches, and resolving conflicts. You'll learn about the HEAD pointer, the current branch, and how branches are represented in Git's object model. The interactive visualizer below will show you branches in action — watch the branch pointers move as you create and switch branches.

Creating and Switching Branches

Branches are lightweight pointers to commits. The git branch command creates a new pointer. The git switch or git checkout command moves the HEAD pointer to a different branch. The HEAD pointer tells Git which branch you're currently working on. When you create a new branch, Git creates a new pointer at the current commit. This means branches are instant — they don't copy any files. This is why branching is so fast in Git. Understanding this helps you see branches as cheap and disposable.

When you switch branches, Git changes the files in your working directory to match the state of that branch. This is how you work on different features in isolation. The git checkout -b command is a shortcut that creates and switches in one step. This is the most common way to start working on a new feature. You'll use this command constantly as a developer. Understanding what's happening under the hood — that you're just moving a pointer — helps you understand why Git is so fast and why branching is so powerful.

In the visualizer above, click the Scenario Tutorials tab and load the Branching & Merging scenario. Watch as the main branch pointer and the feature branch pointer move as you create branches, switch between them, and commit changes. You'll see the commit graph grow and branch pointers move in real-time. This visual representation makes the concept of branches as pointers crystal clear.

Merging Branches

Merging combines changes from two branches. Git merge takes the changes from the target branch and applies them to the current branch. There are two types of merges: fast-forward (when there's a linear history) and three-way (when branches have diverged). Fast-forward merge happens when there are no diverging commits — the target branch is ahead of the current branch. Git simply moves the current branch pointer forward. Three-way merge happens when branches have diverged — Git creates a new commit that combines the changes from both branches.

Understanding these merge types helps you predict what Git will do. A fast-forward merge is simple and doesn't create a merge commit. A three-way merge creates a merge commit with two parents. This is how Git preserves the history of branching and merging. The visualizer above shows both types of merges. Watch the Branching & Merging scenario to see a fast-forward merge in action. You'll see the main branch pointer simply move forward to the feature branch's commit. Then watch as the scenario shows a three-way merge with a merge commit.

Resolving Merge Conflicts

Merge conflicts happen when Git can't automatically merge changes. This occurs when both branches modify the same lines of the same file. Git marks the conflict in the file, and you must resolve it manually. Resolving conflicts requires editing the file to choose which changes to keep. Git adds conflict markers: <<<<<<< HEAD (your changes), ======= (separator), and >>>>>>> branch-name (their changes). Remove these markers and keep the correct version. Then add and commit the resolved file.

Merge conflicts are normal and happen frequently in collaborative development. The key is to resolve them carefully — don't just blindly choose one side. Understand what both changes are doing and combine them appropriately. After resolving, test your changes before committing.

Step-by-Step: Creating and Merging Branches

Follow these steps to practice creating and merging branches. This is a fundamental Git workflow you'll use constantly.

Step 1: Create a repository with a commit
mkdir branching-demo
cd branching-demo
git init
echo <strong><code>Initial content > file.txt
git add file.txt
git commit -m <strong><code>Initial commit
This creates a repository with one commit on the main branch.

Step 2: Create a new branch
git branch feature
This creates a new branch called feature pointing to the same commit as main. Run git branch to see both branches — the current branch (main) will have a star (*) next to it.

Step 3: Switch to the feature branch
git switch feature
Now you're on the feature branch. Run git status to confirm. The HEAD pointer has moved from main to feature.

Step 4: Make a change on the feature branch
echo <strong><code>Feature work >> file.txt
git add file.txt
git commit -m <strong><code>Add feature content
This creates a new commit on the feature branch. Run git log --oneline --graph to see the commit history — you'll see the feature branch has moved forward while main stayed behind.

Step 5: Switch back to main
git switch main
Notice that file.txt now contains only Initial content — the feature changes are gone because you switched branches.

Step 6: Merge the feature branch into main
git merge feature
Since main hasn't changed since you created the feature branch, this will be a fast-forward merge. Git simply moves the main branch pointer to the feature branch's commit. Run git log --oneline --graph to see that main now points to the same commit as feature.

Step 7: Create a conflict scenario
Create a new branch: git branch conflict
Switch to it: git switch conflict
Make a change to the same line: echo <strong><code>Conflict change > file.txt
Commit it: git add file.txt && git commit -m <strong><code>Conflict change
Switch back to main: git switch main
Make a different change to the same line: echo <strong><code>Main change > file.txt
Commit it: git add file.txt && git commit -m <strong><code>Main change
Now merge: git merge conflict
Git will show a merge conflict. Open file.txt and you'll see conflict markers. Edit the file to keep the correct version, remove the markers, then git add file.txt and git commit -m <strong><code>Resolve conflict.

Try It Yourself: Create a third branch called experiment, make several commits on it, then merge it into main. Practice resolving conflicts by intentionally creating them.

Knowledge Check

Ready to test your understanding of 3. Branching: Creating, Switching, and Merging?

Back