Git

How to Git Rebase One Branch on Top of Another Branch

In Git, when users want to combine the commit history of two branches without merging it, the “git rebase” command can be used. The rebasing operation pushes the commits history of one branch at the top of another local branch, and the HEAD pointer moves to it. Basically, it temporarily rewinds the commits on their current working branch.

This study will discuss the method of rebasing one local branch on top of another.

How to Perform Rebase On One Git Branch on Top of Another?

Follow the below-given procedure to rebase Git one branch on top of another branch:

Step 1: Switch to Git Local Repository

First, move to the particular Git repository by providing its path in the “cd” command:

$ cd "C:\Users\nazma\Git\perk3"

Step 2: Show Remote URL List

Then, run the following command to check the remote URL list:

$ git remote -v

Step 3: Download Latest Version of Remote Repository

To download the copy of the updated version of the remote repository, use the “git fetch” command:

$ git fetch

Step 4: View List of Local and Remote Branch

After that, execute the “git branch” command to get the list of local and fetched remote branches:

$ git branch -a

Here, the “-a” option represents all. It can be seen that all branches are displayed, and we have selected the “alpha” branch for further process:

Step 5: View Log History
Now, check the list of the Git repository log history through the “git reflog .” command:

$ git reflog .

Step 6: Rebase Particular Branch

Finally, use the “git rebase” command along with the remote name and selected local branch:

$ git rebase origin/alpha

According to the below-given output, the rebasing is performed successfully:

Step 7: Ensure Rebasing Operation

Lastly, execute the below-stated command to verify the rebasing operation:

$ git reflog .

It can be seen that the “alpha” branch commit is rebased on top of the other branch commit history:

That’s all! You have learned how to rebase one Git branch on top of other local branches.

Conclusion

To rebase Git one branch on top of another branch, first, move to the particular repository. Then, check the remote URL list and download a copy of the updated remote repository. Next, list all the branches, including both remote and local. Now, check the reference log history and execute the “git rebase <remote-name>/<local-branch-name>” command. This study illustrated the method of rebasing one local branch on top of another.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.