rebaseでコミットを整理する
git rebase -i を使う
手順
例) 実現したいこと
A~Cまでをrebaseで修正
BをAに含めたい
code: (sh)
git status
(HEAD -> main) aaaaaaaa Aのコミット
bbbbbbbb Bのコミット
cccccccc Cのコミット
dddddddd Dのコミット
起点になる親コミットをしていしてrebaseをスタート
A~Cまでを修正したいので直前のDを親コミットに指定する
コミットIDの指定 or HEADからn個できる
code: (sh)
git rebase -i dddddddd
or
git rebase -i HEAD~4
rebase -iの表示でコミットの状態を修正
table:table
コマンド 意味
pick コミットをそのまま適用
squash (s) 直前の pick にまとめる(メッセージ編集可能)
fixup (f) 直前の pick にまとめる(メッセージは破棄)
edit (e) コミット内容を修正(途中で止まる)
drop (d) コミットを削除
reword (r) コミットメッセージだけ変更
code: (sh)
# 修正前
pick Aのコミット
pick Bのコミット
pick Cのコミット
# 修正後
pick Aのコミット
squash Bのコミット ← Aコミットに含まれるように修正
pick Cのコミット
# 修正後のイメージ
Aコミット
Cコミット
保存を行うとタイトル編集画面になる
code: (sh)
# This is a combination of 2 commits.
# The first commit's message is:
Aのコミット
# The following commit messages will also be included:
Bのコミット
タイトルの変更を行う場合はThe first commit's message is: のコミットメッセージを変更する
squashの数だけタイトル編集画面は表示される
変更しなくてもok
code: (sh)
# This is a combination of 2 commits.
# The first commit's message is:
AとBのコミットa ← 修正
# The following commit messages will also be included:
Bのコミット
rebase後の状態の確認
code: (sh)
git status
(HEAD -> main) aaaaaaaa AとBのコミット ← BのコミットがAに含まれた状態になっている
cccccccc Cのコミット
dddddddd Dのコミット
修正後は安全に強制push
code: (sh)
git push --force-with-lease --force-if-includes
Gitのrebaseの使いどころ
https://hiroblogdesu.hatenablog.com/entry/2025/05/14/133848
#Git
#GitHub