Git Branches
Learn about Git branches, a powerful mechanism for duplicating commit history. Understand how branches enable parallel development and collaborative work without conflicts.
Branches are a mechanism that allows duplicating the commit history into multiple versions. They are very useful in collaborative work and are one of Git's most powerful features. Their basic use is to separate the changes made by one developer (Developer A) from those made by another developer (Developer B). This way, developers don't interfere with each other's work.
Note:
Branches have more than one purpose, and we will explore additional uses of branches in future articles on collaborative work and advanced Git workflows.
What Branches Are
Let's revisit a diagram to visualize how branches work:
This is what your repository looks like initially. In reality, you are already using a branch called "main," which represents the primary branch. So you make your changes on the "main" branch, and you have a single commit history.
Note:
Historical note: The default branch used to be called "master" but has been changed to "main" in most modern Git configurations for more inclusive terminology.
Let's see what would happen if we created a new branch:
We duplicated the commit history, and now there are two branches. This is very useful to allow developers to work independently without affecting each other's code.
Working on Different Branches
Let's see what happens when we work on this new branch:
Two commits have been added to the history of the new branch. The usefulness of doing this may seem abstract, but you'll fully grasp the benefits of branches when we delve into collaborative work with Git.
Common Branch Use Cases
Merge
There's a feature that gives branches more substance: the merge. With merge, you can combine two histories. You tell Git to take one commit history and add the commits it contains to another history.
In the case where we're working on two separate branches, this is the result of a merge:
Here, we've merged the commit history of the "test branch" into "main". That's what a merge is all about.
Note:
Important: You'll notice that the branch from which we merged the history continues to exist; a merge doesn't involve deleting the branch. This allows you to continue working on the feature branch if needed.
Types of Merges
There are different types of merges in Git:
Note:
Fast-forward merge: When the target branch hasn't diverged from the source branch, Git simply moves the pointer forward.
Three-way merge: When both branches have new commits, Git creates a new merge commit that combines the changes from both branches.
Basic Branch Commands
Here are some essential commands for working with branches:
# List all branches
git branch
# Create a new branch
git branch feature-name
# Switch to a branch
git checkout feature-name
# Create and switch in one command
git checkout -b feature-name
# Merge a branch into current branch
git merge feature-name
# Delete a branch (after merging)
git branch -d feature-name
Branch Workflow Example
Benefits of Using Branches
Note:
Isolation: Each feature or fix is developed in isolation, preventing incomplete code from affecting the main codebase.
Parallel Development: Multiple developers can work on different features simultaneously without conflicts.
Experimentation: You can try new ideas without fear of breaking existing functionality.
Code Review: Branches facilitate code review processes before merging changes into the main branch.
Conclusion
We will delve into more concrete use cases later, particularly when we explore collaborative workflows and advanced Git techniques. This article was meant to provide a brief overview of branches and introduce you to one of their primary use cases.
Understanding branches is crucial for effective Git usage, especially in team environments where multiple developers work on the same project simultaneously.
Summary
- Branches are a mechanism for duplicating commit history into separate development lines
- The main branch (formerly "master") is the primary branch created by default
- Multiple branches allow developers to work independently without interfering with each other
- Merging combines the commit history from one branch into another
- Branches continue to exist after merging unless explicitly deleted
- Branches enable parallel development, experimentation, and feature isolation
Note:
In the next article, we'll explore collaborative work to see how branches facilitate team development and remote repository management.