Understanding the Git Rebase command

In this article I will try to explain the concept of git rebase
and its advantages over git merge
in maintaining a clean project history when working in source control. I will discuss scenarios where rebasing can be useful, such as when a bug is identified in the main branch for instance.
I will elaborate on the different modes of performing a rebase, including standard and interactive modes, which allow developers to have more control over their commits. Overall, the goal of using git rebase
is to achieve a clean and easy-to-read project history.
I myself never really understood what I was doing when exeucting these commands other than just reading what was said in an Atlassian tutorial and hoping for the best, I wanted to change that and learn about this command and in the process record my insights in this article.
The scenario
Imagine that you are a cloud engineer and you work with infrastructure as code or you're an application developer writing an application. You will most likely be familiar with working in a source control system, the most common one is Git. Imagine that you have created a branch where you have submitted a few changes and now you want to submit this code to the main codebase. While you have been working on your feature branch, the main codebase has moved along three or four commits since you started working, so now there is a discrepancy between your branch and the code that you have and the code that is in the main codebase. There is a chance that your code may not work anylonger because of changes into the main codebase. It is therefor important that we incorporate the changes in the main codebase with our feature branch to ensure it is still functional code in there.
How do we fix achieve this
There are a few ways we could solve this, we could perform a git merge
or you could do a git rebase
. Some teams would like you to use rebase instead of a merge and the primary reason is to maintain a linear project history, maybe you are working on an open-source project with hundreds of contributors.
Rebase will prevent you from clogging up the history with a lot of merge commits. The goal that we are trying to achieve here is to make sure that we get our changes into the main codebase, git rebase
will make it look like we have been working of the latest version of the main codebase, creating a linear history.
Different modes of rebase
You can do rebase in standard mode, you can also do it in what is called the interactive mode.
In standard mode we will automatically take the commits that you have in your working branch and just place them at the head of whatever branch you choose to rebase on. So if you choose to rebase on the main branch, your commits will be staged after the main branch. You can run git rebase in interactive mode, and this gives you a little bit more control. Instead of automatically taking all of your commits and just placing them at the head of the main branch, you can change your commits and clean up history if you wish to by removing, splitting or altering an existing series of commits.
An example of the commands I would run in order to incorporate changes in main
with my feature branch disk_encryption_kv
(Taken from a real life example working with some Terraform Modules).
// Switch to the main branch
git checkout main
// Fetch the latest updates from main
git pull
// Switch back to my feature branch
git checkout disk_encryption_kv
// Rebase my commits in my feature branch at the HEAD of main
git rebase main
This means I will have achieved this:

When you do a git merge
, instead of a rebase, you create what is called a merge commit, and this just adds to the history of the project and can muck things up a little bit, however having the information it provides can sometimes be useful as well, which branches has merged with eachother for example.
Merge commits is not necessarily a bad thing, it just depends on what type of project you are working on. If you are for example working in an open source project that has a long history, it is important to keep the history clean and provide valuable information about the project.
Main points
- Git rebase is used to maintain a linear project history and avoid cluttering the logs with merge commits.
- Rebasing helps identify where things went wrong by providing a clean history view with the git log command.
- There are two main modes of performing a git rebase: standard mode and interactive mode, giving developers more control over their commits.
Action-items, things I will read more about
- Experiment with the interactive mode of git rebase to have more control over commits and clean up history if necessary (this week).
References
About me
