IEncinas

How to split a git commit

The need for splitting a git commit isn’t that common, but it isn’t unheard of either. You might need to split commits for several reasons:

Let’s say your latest commit is the one that needs to be split:

 1$ git show
 2
 3commit 00d96c81001b (HEAD -> master)
 4Author: --------------------------------------
 5Date:   Sun Apr 6 16:59:23 2025 +0200
 6
 7    i need to be split
 8
 9 file/file  | 0
10 other/file | 0
11 2 files changed, 0 insertions(+), 0 deletions(-)

In i need to be split we created two (empty) files. Let’s say that we want one commit to add file/file and another one to add other/file instead of making it in one go.

They fastest way to proceed would be the following:

git reset HEAD~1 other/file if we specify a path for git reset it will not move our HEAD.

 1$ git status
 2
 3On branch master
 4Changes to be committed:
 5  (use "git restore --staged <file>..." to unstage)
 6        deleted:    other/file
 7
 8Untracked files:
 9  (use "git add <file>..." to include in what will be committed)
10        other/

will show that we have staged the deletion of other/file (as we wanted) but it has kept it in our working directory. Therefore, the next step would be to git commit --amend. After that, we would have our 1st commit in its final form.

To finish the job, we would need to git add other/file and git commit.