Edit and Organize Your Commits with Git Interactive Rebase

Git rebase interactive is a tool that allows you to edit and rebase commits in your Git history in an interactive way…

Sebastián Vidal Aedo
3 min readMay 6, 2023

Yeah, but why i’m going to need it? Imagine your git log look something like this:

commit e47d1a8b08c9f59e1b662f6b57f26383b67f37f5 (HEAD -> master)
Author: John Doe <johndoe@example.com>
Date: Thu May 5 12:00:00 2023 -0400

Final commit

commit 37f48de4100e157e42d8283d3c8cc7fcd6a0c68b
Author: John Doe <johndoe@example.com>
Date: Wed May 4 15:00:00 2023 -0400

Add feature X

commit 12093df4a78d4b84f05c1fb2a1cfcf10ba86a9c9
Author: John Doe <johndoe@example.com>
Date: Tue May 3 10:00:00 2023 -0400

Fix bug in feature Y

commit f85e2d5c1966ca71755cbbd72f47bf3323e0103a
Author: John Doe <johndoe@example.com>
Date: Mon May 2 09:00:00 2023 -0400

Add feature Y

commit 74d1c5bbf7b6ca936e6d3b36e3c41e3de01f2425
Author: John Doe <johndoe@example.com>
Date: Sun May 1 08:00:00 2023 -0400

Initial commit

You don’t want to share or deliver as a final software product “something” with that log where it can (badly) show a low quality when committing code.

To fix this we use Git Interactive Rebase.

Continuing the example, let’s imagine we want the following: each commit has a sequential text as content (just for the example, in a real case it should be different) and also, we want to merge the last 2 commits into only 1.

git rebase -i HEAD~5

In this case, HEAD~5 indicates that the last 5 commits before the current commit will be included, that is, the last 5 commits in the current branch at the position of the HEAD pointer will be included.

With the previous command, an interactive window will be displayed where you can edit the commits. Something like this:

pick 74d1c5b Initial commit
pick f85e2d5 Add feature Y
pick 12093df Fix bug in feature Y
pick 37f48de Add feature X
pick e47d1a8 Final commit

To rename commits sequentially, change the word “pick” to the letter “r” (which stands for “reward”). It should look like this:

r 74d1c5b Initial commit
r f85e2d5 Add feature Y
r 12093df Fix bug in feature Y
r 37f48de Add feature X
r e47d1a8 Final commit

After saving, we will see windows to update each of the commit messages by updating the commit text (example: commit 1, 2, 3….). We do a

git rebase --continue

and finish the process.

Now, when we do git log we will see something like this:

commit e47d1a8b08c9f59e1b662f6b57f26383b67f37f5 (HEAD -> master)
Author: John Doe <johndoe@example.com>
Date: Thu May 5 12:00:00 2023 -0400

Commit 5

commit 37f48de4100e157e42d8283d3c8cc7fcd6a0c68b
Author: John Doe <johndoe@example.com>
Date: Wed May 4 15:00:00 2023 -0400

Commit 4

commit 12093df4a78d4b84f05c1fb2a1cfcf10ba86a9c9
Author: John Doe <johndoe@example.com>
Date: Tue May 3 10:00:00 2023 -0400

Commit 3

commit f85e2d5c1966ca71755cbbd72f47bf3323e0103a
Author: John Doe <johndoe@example.com>
Date: Mon May 2 09:00:00 2023 -0400

Commit 2

commit 74d1c5bbf7b6ca936e6d3b36e3c41e3de01f2425
Author: John Doe <johndoe@example.com>
Date: Sun May 1 08:00:00 2023 -0400

Commit 1

Finally, to merge the last 2 commits we do:

git rebase -i HEAD~2
pick 37f48de Commit 4
pick e47d1a8 Commit 5

change pick for squash

pick 37f48de Commit 4
s e47d1a8 Commit 5

Then save and close the interactive window. Another window will open so you can edit the resulting commit message. Make sure the message adequately describes the changes made.

When you save and close this window, Git will merge the two commits and show you the result in the commit log. The commit log will look like this:

commit eec98e92e714f44955f7b66e238fb2e025aa1d12 (HEAD -> master)
Author: John Doe <johndoe@example.com>
Date: Thu May 5 12:00:00 2023 -0400

Commit 5

Commit 4

commit 12093df4a78d4b84f05c1fb2a1cfcf10ba86a9c9
Author: John Doe <johndoe@example.com>
Date: Tue May 3 10:00:00 2023 -0400

Commit 3

commit f85e2d5c1966ca71755cbbd72f47bf3323e0103a
Author: John Doe <johndoe@example.com>
Date: Mon May 2 09:00:00 2023 -0400

Commit 2

commit 74d1c5bbf7b6ca936e6d3b36e3c41e3de01f2425
Author: John Doe <johndoe@example.com>
Date: Sun May 1 08:00:00 2023 -0400

Commit 1

As you can see, commits 4 and 5have been merged into a single commit called “Commit 5 - Commit 4”. The hash of commit 5 has also changed due to the change in its history.

--

--