Git

How to Git rebase master onto any Branch by Example

On Git, branching is the process of being separated from the master branch so that developers can work separately without affecting the original code and other team members. When a developer creates a Git repository, by default, it is added to the master branch. However, developers can switch branches whenever they need to with the help of Git multiple commands, such as the “git rebase” command, without losing important information.

In this manual, we will learn how to Git rebase master onto any branch by example and provide Git rebase basic commands with descriptions.

What is Git rebase?

Rebasing is the procedure of pushing or merging a sequence of the existing commits on top of a new base commit. Basically, it is known as the linear procedure of combining. More specifically, Git rebase is the most popular utility designed to merge the changes from Git one branch onto any branch.

Example: How to Git rebase master onto another Git Branch?

To Git rebase master or combine the sequence of commits onto another Git branch, first, open up the “Git Bash” terminal and move to the Git root directory using the “cd” command. Next, execute the “$ git branch -a” command to display all existing branches and select any one of them to rebase. Finally, execute the “$ git rebase master <branch-name>” command to rebase the master onto another branch in Git.

Now, let’s check out the following procedure to view the output of the above-stated scenario!

Step 1: Launch Git Terminal

Search and open up the “Git Bash” on your system using the “Startup” menu:

Step 2: Navigate to Git Root Directory

Next, execute the “cd” command to move inside the Git root directory:

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

Step 3: List All Branches

Now, execute the “git branch” command to display all branches using the “-a” option:

$ git branch -a

As you can see, all current and existing branches are displayed, including remote branches. We will select the “feature” branch of the Git local repository to perform rebase on it:

Step 4: Rebase master onto Another Branch

Lastly, execute the provided command and specify the branch name for rebasing action:

$ git rebase master feature

Below output shows that the “master” is rebased onto the “feature” branch successfully:

Now, move to the next section to check out the Git rebase basic commands with their brief description.

What are Git rebase basic Commands?

Let’s check out the Git rebase basic commands, which are stated in the below-provided table with the description:

Commands Description
$ git rebase –interactive<base> Used to perform the interactive rebase.
$ git rebase<base> Used to perform standard rebase.
$ git rebase –x Used to run the command line shell script for every marked commit during playback.
$ git rebase –d Used to discard the commits from the merged commit block while playback.
$ git status Used to view the Git rebase status.
$ git rebase –p Used to hold the separate commit in the Git branches history.
$ git rebase –skip Used to skip the made changes.
$ git commit -m “commit message for <branch-name>” Used to commit the modifications
$ git add <project file> Used to add a branch to the Git repository.
$ git rebase — continue Used to continue the modifications that are made by users.

This study described the procedure to Git rebase onto any branch with the help of an example and briefly discussed the Git rebase basic commands.

Conclusion

To Git rebase onto any branch, first, move to the Git root directory and list all existing branches, including remotes, with the help of the “$ git branch -a” command. Next, execute the “$ git rebase master <branch>” command to integrate changes from the Git “master” branch to the specified branch. This manual offered the method of rebasing the master onto a branch.

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.