How to Use Git Rebase on the Command Line

John Wachira Feb 15, 2024
How to Use Git Rebase on the Command Line

This article will discuss effectively using the git rebase command. The git rebase command allows us to alter a series of commits and modify the commit history in our repository.

We can edit, reorder, or squash commits using the git rebase command.

Use git rebase on the Command Line

Here are some common usage options.

  1. We can edit a previous commit message.
  2. We can combine two or more commits into one.
  3. We can revert or delete unnecessary commits in our repository.

When rebasing, we can rebase against a branch or against a point in time in our repository.

To rebase against a branch, we run:

$ git rebase --interactive <branch_name>

To rebase against a point in time, we run:

$ git rebase --interacive HEAD~

We can add HEAD~7 to rebase up to the seventh commit.

Let us look at the available commands while rebasing.

  1. pick - We use it to reorder our commit history.
  2. reword - We use it when we want to change the commit message. It does not affect the changes introduced by the commit.
  3. edit - We use it when we want to edit or amend a commit. We can split a commit into smaller commits or remove errors introduced by the commit.
  4. squash - We use it to combine two commits into one and give us a chance to give the new commit a new message.
  5. fixup - It is the same as squash except that it discards the message for the merged commit and uses the one above it.

We will now attempt to use the above options in an example. In this example, we will rebase against a point in time in our Delftscopetech repository.

We will rebase HEAD~7. We run:

$ git rebase --interactive HEAD~7

The text editor shows the output below.

git rebase up to the seventh commit

Our commits are arranged from the oldest to the newest.

In our text editor, we will:

  1. Squash the commit (fa39187) into the commit (1fc6c95), using squash.
  2. Move the last commit (7b36971) up before commit (6b2481b), and keep it as pick.
  3. Merge the commit (c619268) into the commit (6b2481b), and discard the commit message using fixup.
  4. Split (dd1475d) using edit.
  5. Fix the commit message (4ca2acc) using reword.

We will modify the commands in the text editor like the below.

modify the text editor

We can save and close the text editor, and Git will start the rebase.

Git will skip the pick 1fc6c95 and open an editor since the squash requires some input from us. It should look like this.

Git open editor - squash

You can close the editor and continue the rebase if satisfied with the changes. The next three commands do not require our input, but the edit does and prints the below message to the terminal.

Git open editor - edit

At this point, we can make any changes and create a new commit with the git commit --amend command. Once done, we can proceed by running the git rebase --continue command.

Always run the git rebase --continue after making the changes. You cannot rebase later if you forget to run the command and continue coding.

However, you can remedy this by:

  1. Run the git reset --soft HEAD^ command to the HEAD ref to the parent.
  2. Run the git rebase --continue to finish the rebasing.

You can also abort the rebase by running the git rebase --abort command and redo the process. This will require you to resolve the merge conflict again.

The reword part will require our input, and Git opens up the text editor with the information below.

Git open editor - reword

We can change the text, save the file and finish the rebase.

In a nutshell, the git rebase command allows us to change the state of our repository in terms of commits. We can squash, rename, or reorder commits.

Pay attention when merging conflict while rebasing.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Rebase