[git]入門git 5.履歴の改編
コミット履歴の入れ替え
最新3件のリビジョン値とコミットメッセージを表示
git log --pretty=format:"%h %s" HEAD~3..HEAD
出力例
8cb2edb head_second da18f38 body_first 8a5aac0 head_first
順番を入れ替えて、以下のようにしたい
da18f38 body_first 8cb2edb head_second 8a5aac0 head_first
エディタを開く。
git rebase -i HEAD~3
表示されるもの。git logとは逆順になっている。下の方が新しい。
pick 8a5aac0 head_first pick da18f38 body_first pick 8cb2edb head_second
以下のように順番を入れ替えて保存。rebaseが実行される。
pick 8a5aac0 head_first pick 8cb2edb head_second pick da18f38 body_first
再度git logを表示。順番が入れ替わり、リビジョンも変わっている。
8b9bc56 body_first 40399da head_second 8a5aac0 head_first
履歴の圧縮
上記head_firstとhead_secondをまとめる。
変更するリビジョンの一つ前を指定してりベース
(HEAD~3と同じ)
git rebase -i "8a5aac0^"
まとめたいコミットのpickをsquashに変更
pick 8a5aac0 head 1 squash 40399da head 2 pick 8b9bc56 body 1
コミットコメントを求められるので入力すれば完成
修正後のgit log
f0fa31e body_first c619c70 head_all
コミットを複数に分割する
再びrebaseを実行する。
git rebase -i HEAD~3
分割したいコミットをpickからeditに変更する
edit c619c70 head_all pick f0fa31e body 1
以下のメッセージが出てrebaseが停止する
Stopped at c619c70... head_all You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue
HEADの位置がeditを指定したものになっているので、HEADを一つ前に戻す。
git reset "HEAD^"
ファイルを分割したい単位に修正してコミット。
その後以下のコマンドでrebaseを続行させる。
git rebase --continue
以下のメッセージが出れば終わり。
Successfully rebased and updated refs/heads/master
修正後のgit log
9e643e4 body 1 94de979 repiyo 6c5c056 rehoge