Chapter 5. Work With Branches

When you add new features while continuing development on the main codebase, managing multiple versions of the code becomes essential. Git branches enable developers to work on different versions of a project simultaneously without affecting the main development. In this chapter, we’ll explore Git branches, their lifecycle, and key Git commands that facilitate efficient version control.
What We Cover in This Chapter
The following topics are covered in this chapter:
Git Regular Workflow – Work With Branches
In this section, we’ll explore how branches function in a typical Git workflow. We will cover six essential Git commands used for branch operations, including git branch
, git checkout
, git switch
, git merge
, git rebase
, and git stash
.
What Is a Branch?
In this section, we’ll explain the concept of branches in Git. A branch is an independent line of development that records a separate commit history. We'll explore the benefits of branching, including parallel development and efficient collaboration.
Branch Operation Basic Life Cycle
We’ll walk through the fundamental lifecycle of a Git branch, from creation to merging or deletion. This section will help you understand how branches evolve within a repository.
Create Branch and Check Branch Status – Git Branch
In this section, we’ll cover the git branch
command, explaining how to create, check, and delete branches.
Switch Current Branch (1) – Git Checkout
We'll explain how to switch between branches using git checkout
. Additionally, we’ll cover how to create and switch to a new branch in a single command.
Switch Current Branch (2) – Git Switch
This section introduces git switch
, a modern alternative to git checkout
, making branch switching more intuitive.
Merge Branches – Git Merge
We’ll explain how to combine changes from different branches using git merge
. You’ll learn different merging techniques and scenarios.
Fast-Forward Merge
Fast-forward merges occur when no additional commits have been made on the base branch since the feature branch diverged. We’ll illustrate how this process works in Git.
Non-Fast-Forward Merge (No Option)
In this section, we’ll explore the case where the main branch has evolved independently, requiring a new commit to integrate the changes.
Non-Fast-Forward Merge (--no-ff Option)
This section covers the --no-ff
option, which enforces a new commit during the merge, preserving the feature branch’s history.
Squash Merge
Squash merges help streamline the commit history by consolidating multiple commits into a single commit before merging. We’ll walk through the --squash
option.
Rebase Branch – Git Rebase
Rebasing allows developers to move the base of their branch onto another branch’s latest state. This section covers the git rebase
command and its benefits.
Managing Conflict
Merge conflicts arise when Git cannot automatically integrate changes. This section explains how to identify, resolve, and finalize merge conflicts.
Stash Changes – Git Stash
The git stash
command allows you to temporarily save your changes without committing them. We’ll explore how to use stash for handling work-in-progress code.
Learn offline for better focus!
A book for this course is available on Amazon.
Git & GitHub Visual Guide
The Hands-On Manual for Complete Beginners to Master Version Control and Remote Coding Collaboration
Get the Book Now