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
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
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:
- As an alternative merging strategy to
git merge
- As a means of rewriting history
Let us move straight into the demo.
Case Example of git rebase
as an Alternative Merging Strategy
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 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>
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.
Therefore, I want to rewrite the git history of my personal project by using reword on interactive rebase command.
Since we only want to change the latest commit, we only put HEAD~1 on the command
Then the git history would change. Below is a before and after snapshots of the
git log
:Then, in order to push the changes, you could just run:
git push --force