git rebase -i(インタラクティブモード)を利用すると、コミット履歴をきれいに整理できます。例えば、複数のコミットをまとめたり、コミットの順序を変更したり、過去のコミットメッセージを変更したりすることができます。
この記事ではgit rebase -iで複数のコミットをまとめる方法についてわかりやすく解説します。
git rebase -iで複数のコミットをまとめる方法
まず、次のようなコミット履歴があるとします。
$ git log --oneline --reverse
...(略)
220e1ab commit A
5335e3d commit B
df58e03 commit C
2810a5a (HEAD -> main) commit D今回は、git rebase -iで「commit Aとcommit B」及び「commit Cとcommit D」のコミットをそれぞれまとめてみましょう。
手順を以下に示します。
複数のコミットをまとめる手順
git rebase -iコマンドを実行する- テキストエディタでコミットをまとめる
- コミットメッセージを修正する
では上記の手順について順番に説明します。
git rebase -iコマンドを実行する
git rebase -i(または、git rebase --interactive)コマンドを実行します。
git rebase -i HEAD~~~~
#以下のコマンドでも可能
git rebase --interactive HEAD~~~~
git rebase -i HEAD~4
git rebase --interactive HEAD~4上記のコマンドはHEADから4つ分のコミットを対象にリベースを開始するコマンドです。
テキストエディタでコミットをまとめる
git rebase -iコマンドを実行すると、するとテキストエディタ(たいていvim)が開き、HEADから4つ分のコミットが表示されます。
pick 220e1ab commit A
pick 5335e3d commit B
pick df58e03 commit C
pick 2810a5a commit D
# Rebase df00432..2810a5a onto df00432 (4 commands)
(以下略)今回は、「commit Aとcommit B」及び「commit Cとcommit D」のコミットをまとめたいので、以下に示すように、統合先のコミット(残したいコミット)の pick はそのままにして、統合によって消したいコミットの pick を s または squash に変更します(今回はsに変更しています)。なお、文字を変更する際には、まずiキーを押してインサートモードに切り替えます。インサートモードでpickをsに変更したら escキーでインサートモードを終了し、:wqを入力した後、Enterキーを押してテキストエディタを閉じます。
pick 220e1ab commit A
s 5335e3d commit B
pick df58e03 commit C
s 2810a5a commit D
# Rebase df00432..2810a5a onto df00432 (4 commands)
(以下略)コミットメッセージを修正する
先ほどの手順でEnterキーを押すと、以下に示すように、コミットメッセージを編集するテキストエディタが開きます。
# This is a combination of 2 commits.
# This is the 1st commit message:
commit A
# This is the commit message #2:
commit B
# Please enter the commit message for your changes. Lines starting
(以下略)iキーを押してインサートモードに切り替え、必要に応じて、コミットメッセージを修正しましょう。今回は、以下に示すように残したいコミットのコミットメッセージをcommit Aからcommit A and commit Bに変更しました。
# This is a combination of 2 commits.
# This is the 1st commit message:
commit A and commit B
# This is the commit message #2:
# ここを消した!!
# Please enter the commit message for your changes. Lines starting
(以下略)コミットメッセージの修正が終わったら、escキーでインサートモードを終了し、:wqを入力した後、Enterキーを押しましょう。
Enterキーを押すと、再度コミットメッセージを編集するテキストエディタが開きます。
「commit Aとcommit B」及び「commit Cとcommit D」のコミットをまとめているので、コミットメッセージを修正するテキストエディタが2回開きます。「commit Aとcommit B」のみのコミットをまとめた場合、コミットメッセージを修正するテキストエディタは1回しか開きません。
# This is a combination of 2 commits.
# This is the 1st commit message:
commit C
# This is the commit message #2:
commit D
# Please enter the commit message for your changes. Lines starting
(以下略)同様に、コミットメッセージを修正します。今回は、以下に示すように残したいコミットのコミットメッセージをcommit Cからcommit C and commit Dに変更しました。
# This is a combination of 2 commits.
# This is the 1st commit message:
commit C and commit D
# This is the commit message #2:
# ここを消した!!
# Please enter the commit message for your changes. Lines starting
(以下略)これで「commit Aとcommit B」及び「commit Cとcommit D」のコミットをまとめることができました。
では次に、git logコマンドを用いて、実際にコミットがまとまっているかを確認してみましょう。
git logコマンドでコミットがまとまっているかを確認する
git logコマンドを実行してコミットがまとまっているかを確認してみましょう。
$ git log --oneline --reverse
...(略)
b6da499 commit A and commit B # ← コミットのハッシュ値(b6da499...)は新たな値が割り振られている
3260cb9 (HEAD -> main) commit C and commit D # ← コミットのハッシュ値(3260cb9...)は新たな値が割り振られている$ git log --reverse -2
commit b6da4997d6b75fe769ad176146c45718bd25be05
Author: user01 <user01@example.com>
Date: Thu Oct 24 16:30:55 2024 +0900 # ← コミットの変更日時はpickのほう(各squashの1つ前のコミットのほう)になる
commit A and commit B
commit 3260cb929f29f723b5ad79e84a01f1690ed87e8b (HEAD -> main)
Author: user01 <user01@example.com>
Date: Thu Oct 24 16:31:12 2024 +0900 # ← コミットの変更日時はpickのほう(各squashの1つ前のコミットのほう)になる
commit C and commit D上記コマンドの実行結果を見ると、コミットがまとまっていることが分かります。なお、コミットのハッシュ値は新たな値が割り振られます。また、コミットの変更日時はpickのほう(各squashの1つ前のコミットのほう)になります。
本記事のまとめ
この記事ではgit rebase -iで複数のコミットをまとめる方法について、以下の内容を説明しました。
git rebase -iコマンドの実行方法git rebase -i HEAD~nコマンドで、HEADからnつ分のコミットを対象にリベースを開始します。
- 複数のコミットをまとめる方法
pickをsまたはsquashに変更することで、複数のコミットを1つにまとめることができます。
- コミットメッセージの修正
- 統合後のコミットメッセージを編集して、わかりやすい内容に更新できます。
git logコマンドでの確認git log --onelineコマンドで、コミットが正常にまとめられているかを確認します。コミットをまとめると、新しいコミットハッシュ値が割り振られ、変更日時はpickのほうの日時が反映されます。
お読み頂きありがとうございました。