A Brief Explanation of Git Rebase (with Example)

date
Mar 23, 2022
slug
git-rebase
status
Published
tags
Git
Git Rebase
summary
Understanding the git rebase command of Git, what is it and when should one use it, in 5 minutes.
type
Post
Rewriting history, made easy nowadays through having control over medias and presses Git commands 😂
https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase
Rewriting history, made easy nowadays through having control over medias and presses Git commands 😂 https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase
When using git, sometimes we discover ourselves in seemingly a point-of-no-return kind of irreversible mistakes. Whereas we might just created a bad commit that would stain the commit history. This is where git rebase might come in play.

What is git rebase?

Rebasing is the process of moving or combining a sequence of commits to a new base commit.
— git rebase by atlassian.com
An illustration of rebase; Rebasing a feature branch into updated main branch.
https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase
An illustration of rebase; Rebasing a feature branch into updated main branch. https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase
Rebasing is a common way to integrate upstream changes into your local repository. Pulling in upstream changes with Git merge results in a superfluous merge commit every time you want to see how the project has progressed. On the other hand, rebasing is like saying, “I want to base my changes on what everybody has already done.”
— git rebase by atlassian.com
I think the definition and the illustration above is already explained the command well. To summarize, the purpose of rebase command are:
  1. As an alternative merging strategy to git merge
  1. As a means of rewriting history
Let us move straight into the demo.

Case Example of git rebase as an Alternative Merging Strategy

An illustration of rebase as an alternative merging strategy.
https://dev.to/mral_x/git-rebase-and-interactive-rebase-6e2
An illustration of rebase as an alternative merging strategy. https://dev.to/mral_x/git-rebase-and-interactive-rebase-6e2
This example is taken from this well written article by Alex Ferreira, talking about Git Rebase and Interactive Rebase.
Upon encountering this kind of situation, if someone choose to do rebasing over merging, there are two things that could be done. The first one is to run the rebase command in the feature branch.
git switch feature/JIRA-123
git rebase main
This will start the rebase process. If no conflicts are found you should see a success message and your feature branch is now up-to-date with the main branch
— Git Rebase and Interactive Rebase by Alex Ferreira
Ferreira added that this would result in a linear git tree like in the image below, instead of tangled with many branches & soiled with “Merge branch ‘feature/JIRA-123’ commit messages.
The result of running git branch main from the said feature branch
https://dev.to/mral_x/git-rebase-and-interactive-rebase-6e2
The result of running git branch main from the said feature branch https://dev.to/mral_x/git-rebase-and-interactive-rebase-6e2
The second approach is to run the rebase command interactively. This can be done by writing the command below:
git rebase -i <commit hash | HEAD position>
Interactive rebase interface.
Interactive rebase interface.
The rebase command invoker would then be brought into vim/nano editor interface, and could liberately choose which commits he would pick, reword, edit, squash, drop, etc.

git rebase as a Means of Rewriting History

After knowing that we can reword our previously added commits using interactive rebase command, this is a step-by-step of how one could do it.
💡
NOTE: This approach is only advised to be used on a personal projects, not a team-based projects.
First, let’s take a look on this project, whereas I just had a typo on my latest pushed commit.
The ‘update cv * resume’ was supposed to be ‘update cv & resume’.
The ‘update cv * resume’ was supposed to be ‘update cv & resume’.
Therefore, I want to rewrite the git history of my personal project by using reword on interactive rebase command.
Writing down the interactive git rebase command.
Writing down the interactive git rebase command.
Since we only want to change the latest commit, we only put HEAD~1 on the command
notion image
Then the git history would change. Below is a before and after snapshots of the git log:
Before.
Before.
After.
After.
Then, in order to push the changes, you could just run:
git push --force
 

© Fahdii Ajmalal Fikrie 2022