Combine old git commits using GitKraken

Avinash Upadhyaya K R
4 min readApr 3, 2021

Have you ever made commits that could have been a single commit and you realised it later? You’re at the right place. In this story, I am going to demonstrate how you can fix that using GitKraken and also show you how you can do that using the command line as well. By the end, you’ll realise how much easier it is to do such stuff using GitKraken.

You can sign up for GitKraken using https://www.gitkraken.com/invite/kq52Q7ti and download GitKraken using https://www.gitkraken.com/download.

I’m going to assume you know how to use GitKraken for basic git commands like add, commit, pull and push. These are really intuitive with GitKraken.

Here’s how my commit history looks like on the UI -

Initial commit history

I have 3 commits apart from the Initial commit. Let’s suppose I want to combine the second and the third commit because I feel they belong together. We can easily do this using GitKraken.

Before you start, make sure you are on a branch which has HEAD on it.

Select the 2 commits using the Ctrl/Shift key on Windows/Linux.

Select the commits

Now, right click on those commits and select “Squash 2 commits” on the menu that pops up.

Select Squash 2 commits from the menu

That’s it, you’re done 🎉 . GitKraken handled everything that was supposed to be done while squashing the 2 commits. Now the 2 commits will be combined together with the commit message of the older commit. You can change that now by editing the commit message by right clicking on the commit and selecting “Edit commit message” from the menu that pops up.

Select “Edit commit message” from the menu

You can edit the message using the text box on the right side. Enter the updated commit message and click on “Update Message”.

Update message for the squashed commits

Here’s how it will look once everything is done -

Final commit history

Congratulations 🎉! You have successfully combined 2 old commits into 1 using GitKraken. Although we were able to do what we wanted to do, I will also demonstrate how you can do the same thing using command line.

Here’s the output from the git log comment -

Output from git log command

Let’s do the same thing we did with the GUI. We will be squashing the second and the third commit using command line.

Just like with the GUI, you will need to make sure you are on a branch which has HEAD on it.

We will start by using the git rebase command with the interactive flag -i and we will also need the number of commits from the HEAD to the last commit we want to combine. In this case, it is 3. The command would be git rebase -i HEAD~3 . This will open your default command line editor in the same command line window. I am using vim for this.

git rebase output

Now, let’s change pick on line 2 to squash and save the file and exit the editor. This will squash all commits below it and will open another editor for updating the commit message for the squashed commit. The first line will be considered as the commit summary, while the rest will be the commit details. Let’s add a new line at the start of the editor with the message — feat: setup initial files .

New commit message for the squashed commits

Save and exit this editor. You’re done 🎉! Now let’s check git log to verify if the commits were combined (squashed).

Output from git log

Congratulations 🎉! You have successfully combined (squashed) 2 commits using the command line. Personally, I feel it is a lot easier with GitKraken than with the command line, especially if you are a beginner. Feel free to let me know which approach do you think is easier. That’s it for this story, catch y’all on another one👋

I’m going to mention this again in case you missed it —

You can sign up for GitKraken using https://www.gitkraken.com/invite/kq52Q7ti and download GitKraken using https://www.gitkraken.com/download.

If you’re curious about feat: and fix: in my commit messages, you can checkout conventional commits for the commit message conventions.

Thank you for reading and have a great day/night. 😄

--

--