Logo

How to rebase commits to another branch (Git)

Oct 03, 2020

we are going to perform a very simple rebase, where we will introduce a new file, commit that file, make a change to it, and then commit it again so that we end up with two new commits.

Step 1 : The jgit repository can be cloned as follows

git clone https://git.eclipse.org/r/jgit/jgit
cd jgit

Step 2 : Check out a new branch, rebaseExample, which tracks origin/stable-3.1:

git checkout -b rebaseExample --track origin/stable-3.1

Step 3 : Make two commits on the rebaseExample branch, as follows:

echo "Rebase Example" > rebaseExample.txt
git add rebaseExample.txt
git commit -m "My brand new rebaseExample"
echo "Line 2" >> rebaseExample.txt
git add rebaseExample.txt
git commit -m "New Line"

Step 4 : Then, we rebase the change on top of the origin/stable-3.2 branch instead:

git rebase origin/stable-3.2

Step 5 : When you execute git rebase, Git starts by finding the common ancestor of the current HEAD branch and the branch you want to rebase to. When Git finds merge-base, it will find the commits that are not available in the branch you are rebasing onto. Git will simply try to apply those commits one by one.