Learn about Git Rebase, an alternative to the widely used git merge command with the help of examples. Git rebase is a powerful feature of Git that has the ability to rewrite and reorganize your commit history. Git rebase allows you to change the base of your branch.
The software engineering landscape is constantly changing. Staying up to date on new technologies is absolutely critical.
But as developers, sometimes we forget how important it is to dig a bit deeper into the tried and true technologies that are the backbone of software development – such as version control systems like Git.
In this article, we’ll take a look at an alternative to the widely used git merge command. That alternative is git rebase.
Git rebase is a powerful feature of Git that has the ability to rewrite and reorganize your commit history. Git rebase allows you to change the base of your branch.
Unlike merging, which creates a new merge commit and combines the history of both branches, rebasing replays the commits of one branch onto another. This results in a linear commit history, making it easier to read and understand.
Not grokking the difference between git merge and git rebase? Let’s consider an example. You’re working on a new change, so you create a new branch with git checkout -b
. You then commit some changes to your new branch.
Basic commit illustration
In the meantime, someone else working on a separate part of the system merges in their own Pull Request! The git history now looks a bit like this, as they don’t have any context or knowledge of your changes.
Illustration of git, your teammate pushed some new commits.
You finish up your changes with one last final commit. But what do you do next to get it back into the main branch?
Illustration of git, with an additional commit on your branch
Well, you have two options. First up is a command that you're likely familiar with if you've used Git before: git merge. The end result looks a bit like this.
Illustration of git, with an additional merge commit
As you can see, we have an additional commit just for the merge. The history carries on from here, and this is fine! Many coders get on just fine using merge commits. But what if you have a longer-lived branch that you need to keep up to date?
In this case, we’re working on a branch named foo
. Our commits are the ones with the comments, some other thing
, adding test
, and addressing comments.
Take a look at what the history looks like.
Git History with Merging
Those Merge branch ‘main’ into foo
commits don’t seem very helpful - they’re simply logistical, and they don’t actually describe changes that you’re making on your branch. It’s also a bit hard to reason about the consecutive changes you made on your current branch when looking at your Git history.
Let’s explore the alternative with git rebase. Taking the same example above, let’s start with our final commit right before we’re ready to merge back into main
.
The base case
This time, we’re going to do a git rebase. This will actually replay your commits on top of the new commits (known as the HEAD) of main
, preventing merge commits.
Rebasing illustration
So how do we accomplish this? While on your side branch (in our case, foo
), you’ll want to do the following.
main
branch with git checkout main
main
branch with git pull
. This is so we have the latest HEAD of main
available for the rebase.foo
with git checkout foo
git rebase main
, this will complete the rebase, replaying your commits on top of the HEAD of main
.In comparison to the earlier example, what does this look like with the same commits “some other thing”, “adding test”, and “addressing comments”?
Cleaner commit history with git rebase
Very nice! It’s cleaner, and looking at my branch history, it’s pretty straightforward to reason about the changes on my branch. I only need to know where I started (commit hash 2a2db46
).
While git rebase undoubtedly keeps your commit history cleaner, there are a few things to be mindful of:
While it's easy to get caught up in new and exploding technologies such as artificial intelligence tooling and edge computing, it's also important for us as developers to take a step back and make sure we are using tried and true tools such as Git to their maximum potential.
In this article, we took a look at git rebase
, and gave an example of why rebasing could be a good tool in your toolbox.
Happy rebasing!
Source: https://www.freecodecamp.org
#git #github