2023/04/26
intaractive rebaseを使いましょう。
gitでコミット分割の方法を調べたのでメモしておきます。
git log --oneline83f52e6 (HEAD -> main) hogef1cae65 fugab8929a2 piyo0fd0f4e hoge945b51e fuga18f60e3 piyod6b8ce8 poyo
下から2番目の18f60e3のコミットを分割したいとします。
このとき、18f60e3,d6b8ce8の2コミットをのちに入力できるように記憶しておきます。
git rebase -i d6b8ce8
とするとエディタが開き、そこには
pick 18f60e3 piyopick 945b51e fugapick 0fd0f4e hogepick b8929a2 piyopick f1cae65 fugapick 83f52e6 hoge# Rebase d6b8ce8..83f52e6 onto d6b8ce8 (6 commands)(以下省略)
のような記述がなされています。
今回いじりたいのは18f60e3なので、
edit 18f60e3 piyopick 945b51e fugapick 0fd0f4e hogepick b8929a2 piyopick f1cae65 fugapick 83f52e6 hoge# Rebase d6b8ce8..83f52e6 onto d6b8ce8 (6 commands)(以下省略)
と文頭のpickをeditに変え、保存して閉じます。
Stopped at 18f60e3... piyoYou can amend the commit now, with git commit --amendOnce you are satisfied with your changes, run git rebase --continue
と出力されれば成功。18f60e3が編集可能となりました。
現在、18f60e3での変更はヒストリー上にあるので、コミットし直すためにワーキングディレクトリまで引き戻します。
git reset HEAD~
git add, git commitで変更をコミットします。
ここでコミットを小分けにすることで、当初の目的であるコミット18f60e3の分割が叶います。
コミットが完了したら、rebaseで編集モードを終了し、HEADへチェックアウトします。
git rebase --continueSuccessfully rebased and updated refs/heads/main.
過去のコミットを分割しなおすことができました。