日本語

How to Squash Multiple Commits into One with git rebase -i

​​Git​command line

When you have a series of small commits such as "fix a typo" or "add a photo" in the middle of your work, you can combine them into one commit with squash in git rebase -i. Use this when you want a cleaner history before opening a pull request or asking for a review. I cover how to edit the todo list, how to combine commit messages, fixup (which drops the message), --autosquash (which combines commits automatically later), and how to undo a squash, all with output from a test environment.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables. Instead of operating an editor by hand, I used small shell scripts that display what the editor receives and then rewrite it. These scripts are set in the environment variables GIT_SEQUENCE_EDITOR (for the todo list) and GIT_EDITOR (for the message). The commands in the body omit these environment variable settings. I did not test operating real editors such as Vim or VS Code, Windows or macOS, conflicts that occur during a rebase, or squashing commits that have already been pushed.

The history before squashing

I start with a history where four commits sit on top of the first commit, created while I was building a profile page. I keep the bottom commit (the first commit) and combine the four above it into one commit, which I call "add profile page."

console
$ git log --oneline
a78c54d 写真を追加
1978f6c 誤字を直す
9dd8530 自己紹介を書く
2f3d332 プロフィールページを追加
1255800 最初のコミット

Open the todo list with git rebase -i HEAD~4

The commits I want to combine are the newest four, so I run git rebase -i HEAD~4. HEAD~4 refers to "four commits before HEAD" (here 1255800), and the commits after it become the targets. The editor opens a todo list like the following (I omitted some of the # explanation lines).

console
$ git rebase -i HEAD~4
pick 2f3d332 プロフィールページを追加
pick 9dd8530 自己紹介を書く
pick 1978f6c 誤字を直す
pick a78c54d 写真を追加

# Rebase 1255800..a78c54d onto 1255800 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
(中略)
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
(後略)

The todo list is in the reverse order of git log --oneline: older commits are at the top. The lines run from top to bottom.

To combine them, I keep the first line as pick, change the lines after it from pick to squash (or s for short), then save and close the editor. squash means "combine into the commit just above" (see the git rebase reference on combining commits).

text
pick 2f3d332 プロフィールページを追加
squash 9dd8530 自己紹介を書く
squash 1978f6c 誤字を直す
squash a78c54d 写真を追加
Warning

If you delete a line from the todo list, that commit is lost. The explanation lines also say If you remove a line here THAT COMMIT WILL BE LOST. Don't delete the lines for the commits you want to combine. Change pick to squash instead.

Write the message for the combined commit

When I close the todo list, an editor for the commit message opens. It shows the messages of the four commits in order from top to bottom.

console
# This is a combination of 4 commits.
# This is the 1st commit message:

プロフィールページを追加

# This is the commit message #2:

自己紹介を書く

# This is the commit message #3:

誤字を直す

# This is the commit message #4:

写真を追加

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Nov 11 10:10:00 2026 +0900
(後略)

Lines that start with # are not included in the message. As it is, this is just the four messages listed together, so I rewrite it into the form I want to keep. In the test, I replaced it with the following message and saved.

text
プロフィールページを追加

写真と自己紹介を載せる

After I save and close the editor, the rebase runs to the end.

console
[detached HEAD 9729791] プロフィールページを追加
 Date: Wed Nov 11 10:10:00 2026 +0900
 1 file changed, 3 insertions(+)
 create mode 100644 profile.html

Successfully rebased and updated refs/heads/main.

Check the history and contents after squashing

The four commits are now one, with the new hash 9729791.

console
$ git log --oneline
9729791 プロフィールページを追加
1255800 最初のコミット
Before squash, four commits sit on top of the first commit. After squash, only one commit, 9729791, sits on top of it
History before and after squash. The top four commits become one commit

With --format=fuller, the author date (AuthorDate) is still 10:10. This is the date of the "add profile page" commit that I kept as pick at the top of the todo list. The commit date (CommitDate) is 11:00, the time I ran the rebase. The message is the one I rewrote in the editor.

console
$ git log -1 --format=fuller
commit 972979110edc2847cd951b0bb8ce63934d74a3f4
Author:     Neko <neko@example.com>
AuthorDate: Wed Nov 11 10:10:00 2026 +0900
Commit:     Neko <neko@example.com>
CommitDate: Wed Nov 11 11:00:00 2026 +0900

    プロフィールページを追加
    
    写真と自己紹介を載せる

In the test, I ran git tag before-squash on the latest commit of the branch (a78c54d) before squashing. Running git diff between the states before and after squashing shows nothing. Only the way the history is divided has changed; the file contents are the same.

console
$ git diff before-squash HEAD

$ cat profile.html
<h1>プロフィール</h1>
<img src="neko.png" alt="猫">
<p>自己紹介</p>

Return to the state before squashing with ORIG_HEAD

When a rebase starts, the latest commit of the branch before the rebase is recorded in ORIG_HEAD (see the git rebase reference). If you notice you combined commits the wrong way, you can go back right away with git reset --hard ORIG_HEAD.

console
$ git log --oneline -1 ORIG_HEAD
a78c54d 写真を追加

$ git reset --hard ORIG_HEAD
HEAD is now at a78c54d 写真を追加

$ git log --oneline
a78c54d 写真を追加
1978f6c 誤字を直す
9dd8530 自己紹介を書く
2f3d332 プロフィールページを追加
1255800 最初のコミット

git reset --hard discards uncommitted changes to files that Git tracks (see the git reset reference). Before you run it, use git status to check that you don't have changes you want to keep.

Combine without keeping the message with fixup

For a commit whose message doesn't need to be kept, such as "fix a typo," use fixup (or f for short) instead of squash. The message of a commit marked fixup is discarded, and only the message of the pick line above it remains (see the git rebase reference).

text
pick 2f3d332 プロフィールページを追加
fixup 9dd8530 自己紹介を書く
fixup 1978f6c 誤字を直す
fixup a78c54d 写真を追加

When I ran this todo list, the rebase finished without opening the message editor.

console
Successfully rebased and updated refs/heads/main.

$ git log --oneline
8e94b10 プロフィールページを追加
1255800 最初のコミット

$ git show --stat --format=%B HEAD
プロフィールページを追加


 profile.html | 3 +++
 1 file changed, 3 insertions(+)

The contents are the same as with squash, but the message is different, so the hash is different: 8e94b10. In the test, I then ran git reset --hard before-squash to return to the five commits from before squashing.

Make the fix as a separate commit first, then combine it with –autosquash

Sometimes another commit comes in after the commit you want to fix. In the following history, I want to change the spelling from the hiragana form to the kanji form of "katsuobushi" (bonito flakes) in the favorites line, but the "add contact page" commit comes after the "add favorites" commit.

console
$ git log --oneline
7dc8cf5 連絡先ページを追加
7a2cf85 好きなものを追加
a78c54d 写真を追加
1978f6c 誤字を直す
9dd8530 自己紹介を書く
2f3d332 プロフィールページを追加
1255800 最初のコミット

I commit the fix with git commit --fixup=<commit to fix>. Here the target is the commit just before HEAD, "add favorites," so I specified HEAD~1. This creates a commit whose subject starts with fixup! followed by the subject of HEAD~1.

console
$ sed -i 's/かつおぶし/かつお節/' profile.html

$ git add profile.html

$ git commit --fixup=HEAD~1
[main a1f679a] fixup! 好きなものを追加
 1 file changed, 1 insertion(+), 1 deletion(-)

When you later run git rebase -i --autosquash, the fixup! commit moves directly below the commit it targets, and the todo list opens with it already changed to fixup (see the –autosquash description).

console
$ git rebase -i --autosquash HEAD~4
pick a78c54d 写真を追加
pick 7a2cf85 好きなものを追加
fixup a1f679a fixup! 好きなものを追加
pick 7dc8cf5 連絡先ページを追加
(後略)

In the original order, a1f679a was above the "add contact page" commit. Now it has moved to just below "add favorites." I don't need to reorder the lines by hand. When I close the editor as is, the fix is merged into "add favorites," and the fixup! commit disappears.

console
$ git log --oneline
5b94894 連絡先ページを追加
27cc2bf 好きなものを追加
a78c54d 写真を追加
1978f6c 誤字を直す
9dd8530 自己紹介を書く
2f3d332 プロフィールページを追加
1255800 最初のコミット

$ git show --format=%s HEAD~1
好きなものを追加

diff --git a/profile.html b/profile.html
index 69a4c8b..5ba3022 100644
--- a/profile.html
+++ b/profile.html
@@ -1,3 +1,4 @@
 <h1>プロフィール</h1>
 <img src="neko.png" alt="猫">
 <p>自己紹介</p>
+<p>好きなもの:かつお節</p>

If you want to keep the message as well, use --squash=<commit> instead of --fixup. This creates a commit whose message starts with squash!, and --autosquash arranges it as squash (see the –squash description for git commit and the –autosquash description for git rebase).

Include the first commit with –root

HEAD~N can't be used when N is greater than the number of commits you can go back through. The history above has seven commits, so HEAD~7 doesn't exist and causes an error.

console
$ git rebase -i HEAD~7
fatal: invalid upstream 'HEAD~7'

To include the first commit in the todo list, add --root (see the –root description). The first commit then appears on the first line of the todo list.

console
$ git rebase -i --root
pick 1255800 最初のコミット
pick 2f3d332 プロフィールページを追加
pick 9dd8530 自己紹介を書く
pick 1978f6c 誤字を直す
pick a78c54d 写真を追加
pick 27cc2bf 好きなものを追加
pick 5b94894 連絡先ページを追加
(後略)

Here I closed the list with everything still as pick, so the history did not change.

If the rebase stops midway, go back to the start with –abort

When you write edit in the todo list, or when changes conflict partway through combining commits, the rebase stops in the middle (see the git rebase reference). git status shows the stopped state.

console
$ git status
interactive rebase in progress; onto a78c54d
Last command done (1 command done):
   edit 27cc2bf 好きなものを追加
Next command to do (1 remaining command):
   pick 5b94894 連絡先ページを追加
(後略)

When I ran git rebase --abort here, the branch returned to the state it was in before the rebase started.

console
$ git rebase --abort

$ git log --oneline -1
5b94894 連絡先ページを追加

$ git status --short --branch
## main

Every commit you squash is recreated with a new hash. In the "Rewriting History" section of Pro Git (Japanese edition), the book warns that you should not rebase commits that have already been pushed to a shared repository.

​Posted by 86