HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Split a Git commit into multiple commits

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
intogitcommitmultiplecommitssplit

Problem

I've often found myself, especially when working on larger feature branches, in a situation where I've made a commit that should have been split into multiple smaller commits. This can happen for various reasons, such as including unrelated changes in a single commit or realizing that a commit should have been broken down into smaller, more focused commits.
While it might sound a little intimidating, it's a fairly straightforward process so long as you're familiar with Git's interactive rebase feature. All you need to do is find the commit you want to split and then mark it with edit to stop the rebase process. Once you've done that, you can use git reset HEAD^ to unstage your changes and then commit them separately. Let's take a look at an example.
```shell title="git-rebase-todo"
edit 3050fc0de Fix network bug
pick 7b1e3f2a2 Update README

Solution

# 1. Use git rebase -i <commit>
git rebase -i HEAD~2


```shell title="git-rebase-todo"
edit 3050fc0de Fix network bug
pick 7b1e3f2a2 Update README
shell
git reset HEAD^
git add src/server/network.js

Code Snippets

# 1. Use git rebase -i <commit>
git rebase -i HEAD~2

Context

From 30-seconds-of-code: split-commit

Revisions (0)

No revisions yet.