GIT REBASE

Ranjeet Jadhav
3 min readMar 22, 2023

Introduction:

Git rebase is a powerful tool that is often used by developers to improve their git workflow. In this blog, we will discuss what git rebase is, and how it can be helpful for developers.

What is Git Rebase?

Git rebase is a technique that allows developers to combine changes from one branch to another by reapplying the changes on top of the target branch. The goal of rebasing is the same as merging, but the result is a cleaner and more linear git history.

Git Merge vs Git Rebase:

When we merge the feature branch to the main branch, all the commits are combined into one commit and merged into the main branch. But when we do git rebase, feature branch commits are put on top of the main branch as it is. This creates a cleaner and more linear git history.

Practical Example:

To better understand git rebase, let’s walk through a practical example.

Step 1: Clone the repository and add commits to the main branch

  • Clone the repository using the git clone command.
git clone https://github.com/REPO_NAME.git
cd REPO_NAME
  • Add two empty files to the main branch using the touch command.
touch a b
  • Commit those files to the main branch.
git add a
git commit -m "a"
git add b
git commit -m "b"
  • Check the commit history using the git log command.
git log --oneline

Step 2: Create a feature branch and add commits to it

  • Create a feature branch from the main branch.
git checkout -b feature
  • Add two files to the feature branch.
touch d e
  • Commit those files to the feature branch.
git add d
git commit -m "d"
git add e
git commit -m "e"
  • Check the commit history using the git log command.
git log --oneline

Step 3: Rebase the feature branch to the main branch

  • Switch to the feature branch using the git checkout command.
git checkout feature
  • Rebase the feature branch to the main branch using the git rebase command.
git rebase main
  • Switch to the main branch using the git checkout command.
git checkout main
  • Rebase the feature branch into the main branch using the git rebase command.
git rebase feature
  • Check the commit history using the git log command.
git log --oneline

In the above image we are seeing that after rebase all the commits of the feature branch came on top of the main branch one by one.

Conclusion:

In conclusion, git rebase is a powerful tool that can be used to improve your git workflow. By using git rebase instead of merge, you can create a cleaner and more linear git history. We hope this blog has helped you understand git rebase better.

--

--