DEV Community

Cover image for A step-by-step guide to splitting commits using Git rebase
Axel Navarro for Cloud(x);

Posted on

5

A step-by-step guide to splitting commits using Git rebase

In the previous posts we checked how to use the git rebase --interactive and how to squash your commits using the fixup command in the git-rebase-todo file.

Now, we're going to learn how to split commits using the terminal. But why splitting a commit? This helps to organize your work. For instance, you may need to rearrange your changes to make backporting hotfixes to older released versions easier, likely using git cherry-pick. Let’s dive in!

Splitting commits

You can use git rebase to split your commits. There are several alternatives to do it and we're going to check the git rebase --interactive way. You can use git rebase --interactive main to reapply all your new commits to the top of the main branch, or just git rebase --interactive HEAD~2 to apply the latest 2 commits.

pick 0680aa7 fix header responsiveness
exec npm test
edit 68ab202 fix grid responsiveness and upgrade bootstrap
exec npm test
break
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ You can pick the rebased commits in any order! but this can cause Git conflicts 😞. Also you can pick commits from other branches like git cherry-pick.

Well, Git will pick the commit 0680aa7 as-is and run npm test and if the command exists then Git will pick the commit 68ab202 and break, returning the control of the shell to you allowing to edit this last commit. Let's see how to edit the last commit.

🧠 To clarify: edit stops after applying the commit, so that you can edit the files or the commit message, amend the commit, and continue rebasing.

First, undo it keeping all the changes in the working tree using the following command:

git reset HEAD^
Enter fullscreen mode Exit fullscreen mode

Now, you are able to make any edition to your code. Then you can use git add to add the changes to the index to commit it in parts splitting the original commit. As the following example shows:

git add package.json package-lock.json
git commit -m 'upgrade bootstrap'
git add .
git commit -m 'fix grid responsiveness'
git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Git will continue running the second exec command that makes an npm test. If this fails you will need to fix your code; otherwise Git will stop at the break command returning the control of the shell to you again. Here you are able to run your application, make the final checks, etc.

If you're happy with the changes, run git rebase --continue to finish the rebase, or git rebase --abort to cancel all changes. πŸ‘€

Let me know in the comments πŸ‘‡ what other method you prefer to split or edit commits.

Conclusion

Remember that it's useful to keep your commits small and semantic. Committing atomic changes makes it easier to check where a crazy bug was introduced. Splitting commits is an easy process to perform and helps you to keep your Git history more readable. Remember, more commits is not an issue if they are small and semantic.

In the next post we're going to check the git rebase --force-rebase command. See ya!

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay