Git Rebase

Learn via video courses
Topics Covered

Overview

The git rebase command moves the feature branch to the tip of the master branch. The git rebases command is an alternative to the git merge command, also used to merge or combine two branches. The git rebases command makes the commits merge linearly. On the other hand, the git merge command will merge the commits in time altogether.

Git also provides an interactive merging. The git interactive rebasing is a tool used to edit, reorder, rewrite, etc., the current commits. We can only perform the interactive git rebasing on the current branch. The rebase command will check the root commit and perform a series of commits, one after the other, at the tip of the master branch commit.

Git also provides us with the advanced rebasing option. We can use the --onto command, and the rebase to activate a more powerful git rebasing.

Pre-requisite

The prerequisites for learning the git rebase command can be a basic understanding of Version Control Systems, Branching, and Git.

Let us discuss them briefly before learning about the git rebase command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a specific code base (or project), etc.

There are two types of Version Control systems, namely.

  • Centralized Version Control Systems (CVCS) and
  • Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a specific code base (or project), etc. Git is free and one of the most widely used version control systems. We can use git through the command line and its graphical user interface (GUI). The command line or terminal version of

Git is known as Git Bash on the other hand the GUI version of git is known as Git GUI. Git tracks the changes in a project and saves a particular state known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a particular commit if we want.


Branching

A branch is an independent line of development used to add specific features and fix bugs without hampering the main project. So, we can develop new features in parallel, and when the development is completed, we can add them back to the main project.

The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

What is Rebase in Git?

The git rebase command moves the feature branch (a development branch) to the tip of the master branch (or main branch). The git rebases command is an alternative to the git merge command, also used to merge or combine two branches. Using the git rebase command, we perform a series of commits from various branches into a final commit.

Git rebase is a crucial command mainly used in the development phase. As we have discussed, we create branches to perform parallel development. Now once the development is done, we commit the changes. We use the git rebase command to merge these commits at the master branch's tip. Refer to the image provided below for better visualization.

git-rebase-in-git

To learn about the difference between the git rebase command and the git merge command, refer to the Git merge vs rebase section.

How to Rebase?

Let us now learn how the rebasing is done in git (step by step).

Suppose we have an ongoing project and have created another branch (namely the test branch) for the development process. We have some commits made in the master branch and the test branch. Now we want to rebase the commits of the test branch to the master branch; we can run the following git rebase command.

Now, if we want to continue with the changes made, then we can run the command:

On the other hand, if we are not happy with the current change and we want to ship this change,

Then we can run the command:

Rebase Branch

Let us now learn about the rebase branch. Suppose that we have a situation where we have several commits in various branches, and we aim to merge them into one. Now for merging them, we can perform git merge or rebase these branches.

The ideal thing is to perform a git rebase, as rebase will align the commits at the tip of the master branch commit linearly.

Rebase Commands

We have three rebase commands in git. Let us see them one by one.

  1. git rebase <base-branch>:
    This command performs the standard rebasing on the specified branch.
  2. git rebase --d:
    This command is used to discard the commit from the final combined commit during the playback.
  3. git rebase --p:
    This command is used to keep the commit alone, i.e. it won't modify the commit's message and will keep the commit as an individual commit.
  4. git rebase --x:
    This command runs command line shells script for the desired commits during the payback.
  5. git rebase --continue:
    This command is used to continue with the changes made in the commit.
  6. git rebase --skip:
    This command is used to skip the changes made in the commit.
  7. git rebase – interactive <base-branch> :
    This command is used to perform the interactive rebasing. (Refer to the next section for details).

Git Interactive Rebase

Let us discuss interactive git rebase. The git interactive rebasing is a tool used to edit, reorder, rewrite, etc., the current commits (basically modify the commits). We must know that we can only perform the interactive git rebasing on the current branch (which we are currently working on).

To perform interactive git rebasing, we can use the git rebase command along with the -i flag, here the -i flag stands for interactive.

The command of interactive git rebase.

If we use the above command, git Bash will open an editor for us. Refer to the image provided below.

interative-git-rebase-in-git

We can see a lot of options contained in the editor. Some of the useful options are:

  1. Pick (-p):
    It is used to include the commits as, by default, we are provided with the table of commits arranged in ascending order (by date). We can rearrange the pick command to change the order of the commits before starting the rebasing.
  2. Reword (-r):
    It is used to halt the rebasing, allowing us to change the commit message before continuing the rebasing.
  3. Edit (-e) :
    It helps us to edit the commit.
  4. Squash (-s):
    It helps us combine two or more commits into a single commit. It also allows us to specify a new commit message to the changes.
  5. Fixup (-f):
    It is used to discard the commit message and use the older commit message for the changes.
  6. Exec (-x):
    It runs shell-related commands against a commit.
  7. Break (-b):
    It stops rebasing at a position.
  8. Drop (-d):
    As the name suggests, it is used to drop the current commit.
  9. Label (-l):
    It is used to specify or add a name to the position of the current head.
  10. Reset (-t):
    It is used to reset the head of the commit to a label.

Git Rebase Standard vs Git Rebase Interactive

As we know, the git interactive rebasing is used to edit, reorder, rewrite, etc., the current commits. On the other hand, the normal git rebasing is used to move the feature branch (a development branch) to the tip of the master branch. Both commands can be used as alternatives to the git merge command.

The significant difference between the interactive git rebasing and normal rebasing is that if we perform the normal rebasing, the rebase command will check the root commit and perform a series of commits, one after the other, at the tip of the master branch commit. On the other hand, if we perform the interactive git rebasing, then the rebase command will open a shell editor and allow us to change and modify the commits.

This can also be visualized as we cannot (user cannot) provide feedback during commits in the normal git rebasing but in the interactive git rebasing, we can provide feedback as well as perform several functions like commit reordering, commit removal, etc. (discussed above).

Git Advanced Rebase Application

We can use the --onto command, and the rebase to activate a more powerful git rebasing. This command lets us pass some specific refs that will become the tip of the rebase (head of the rebase).

The command used for the same is:

Git Merge vs Rebase

As we have discussed earlier, the git rebase command can be used as an alternative to the git merge command. But there are some differences between both commands; let us learn some of the important differences between them.

The major difference between the git rebase, and git merge is that when we perform git rebase, the commits will be merged linearly; on the other hand, if we use the git merge command, all the commits will be merged in time altogether.

git rebase commandgit merge command
The git rebasing is performed linearly.git merging is done simultaneously as a single commit
The git rebase command creates an easily understandable history.Git merge command also creates history but in a graphical form that can be hard to understand.
The git rebase command provides us with several options before merging.Git merge command does not provide us with several options before merging.
The git rebase is used to deal with severe options of merging.Git merge command merges two safe branches.
We should not use the git rebase command on public branches.We can use the git merge command on private and public branches.
The git rebase command rewrites the merging history.The merge command preserves the merging history.
The git rebase command shows us the merge conflicts one after the other.The git merge command shows us the merge conflicts altogether.
The git rebase command does not create any commit at the time of rebasing.The git merge, on the other hand, creates a final commit at the time of rebasing.

Conclusion

  • The git rebase command is used to move the feature branch to the tip of the master branch. The git rebase command is an alternative to the git merge command, also used to merge or combine two branches.
  • The git interactive rebasing is a tool that is used to edit, reorder, rewrite, etc. the current commits. We can only perform the interactive git rebasing on the current branch.
  • The rebase command will check the root commit and perform a series of commits, one after the other, at the tip of the master branch commit.
  • The interactive git rebase command will open a shell editor and allow us to change and modify the commits.
  • We can use the --onto command, and the rebase to activate a more powerful git rebasing.
  • The git rebase command merges the commits linearly; on the other hand, the git merge command will merge the commits in a time.
  • The git rebase command does not create any commit at the time of rebasing; conversely, the git merge creates a final commit at the time of rebasing.
  • We should not use the git rebase command on public branches, but we can use the git merge command on private and public branches.
  • The git rebase command creates an easily understandable history, but the history created by the git merge command is in a graphical form that can be hard to understand.
  • The shell editor of the git rebase interactive command provides us various options like: pick, reword, edit, squash, fixup, exec, break, drop, label, and reset.