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.
- We can edit a previous commit message.
- We can combine two or more commits into one.
- 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.
pick
- We use it to reorder our commit history.reword
- We use it when we want to change the commit message. It does not affect the changes introduced by the commit.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.squash
- We use it to combine two commits into one and give us a chance to give the new commit a new message.fixup
- It is the same assquash
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.
Our commits are arranged from the oldest to the newest.
In our text editor, we will:
- Squash the commit (
fa39187
) into the commit (1fc6c95
), usingsquash
. - Move the last commit (
7b36971
) up before commit (6b2481b
), and keep it aspick
. - Merge the commit (
c619268
) into the commit (6b2481b
), and discard the commit message usingfixup
. - Split (
dd1475d
) usingedit
. - Fix the commit message (
4ca2acc
) usingreword
.
We will modify the commands in the text editor like the below.
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.
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.
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:
- Run the
git reset --soft HEAD^
command to theHEAD
ref to the parent. - 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.
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.
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