Branching and Merging
Introduction
In this guide, we’ll dive into one of Git’s most powerful features: branching and merging. A branch is a feature in Git which allows you to work on features of your project simultaneously. When you’ve completed working on a feature, you can commit your change and merge the commit into the primary branch. Sometimes, developers will work on the same project simultaneously, and their changes will conflict with eachother. In these cases, a merge conflict will need to be resolved.
Pre-requisites: You should already be familiar with Git and how to create a repository and commit. If not, see Getting Started.
What Are Git Branches?
A branch in Git is line of development within your project. It contains a collection of commits. A Git repository will usually contain a primary branch, usually called main
. This primary branch contains your main codebase. A Git repository can also contain other branches, usually called “feature branches” and named after the feature the branch contains. For example, if you are developing a new feature which enables a date range filter, you might call the branch date-range-filter
. Branches are incredibly useful because they enable you to work on new features, bug fixes, or experiments without affecting the main codebase.
Creating a New Branch
To create a new branch, use the git branch
command followed by the branch name. For example, to create a branch named date-range-filter
, you’d run:
git branch date-range-filter
Switching Between Branches
You can switch between branches using the git checkout
command. For instance, to switch to the date-range-filter
branch, you can run the command:
git checkout date-range-filter
Similarly, you can return to the main
branch by running the command:
git checkout main
Creating and Switching to a New Branch in One Step
You can combine branch creation and switching in one step with the -b
flag:
git checkout -b date-range-filter
Making Changes on a Branch
Once you’re on a branch, you can make changes to your code. These changes won’t affect the main branch until you decide to merge them. We’ll cover how to merge a branch later in this tutorial.
Committing Changes
As usual, commit your changes using the git commit
command. Your commits are specific to the branch you’re on.
git add .
git commit -m "Added date range filter"
Merging Branches
When you’re satisfied with the changes on your branch and want to incorporate them into the main branch, you’ll perform a merge.
Merging Into the Main Branch
-
First, switch to the main branch:
git checkout main
-
Then, use the
git merge
command to bring the changes from your branch into the main branch:git merge date-range-filter
Resolving Merge Conflicts
Sometimes, Git may not be able to automatically merge changes, resulting in a merge conflict. In this case, Git will mark the conflicting areas, and you’ll need to manually resolve them. Open the conflicted file(s), choose which changes to keep, and delete the conflict markers (usually <<<<<<<
, =======
, and >>>>>>>
). We’ll cover more complicated cases on resolving merge conflicts in a separate tutorial.
After resolving conflicts, add and commit the changes:
git add .
git commit -m "Resolved merge conflict"
Deleting a Branch
Once you’ve merged changes from a branch, you can delete it if it’s no longer needed:
git branch -d date-range-filter
Conclusion
Branching and merging in Git offer powerful ways to manage code changes and collaborate effectively. By isolating your work on branches, you can experiment, develop new features, and fix bugs with confidence. When you’re ready, merging your branches into the main codebase keeps your project moving forward.