背景

システムの構築が一段落したので、ちゃんと変更管理をしようと考えました。

いままでは、途中の状態を保存するため、細かくコミットしており、意味のないコミットが積み重なっていました。

そこで、積み重ねた意味のないコミットをまとめたいということで、やってみました。

ただ、色々と失敗したので、それも含めて記録します。

やったこと

状態確認

現状の確認。git log --onelineでこれまでのコミットを確認します。WIP(WorkInProgress)のコミットが多いです。

git log --oneline
905c97d (HEAD -> main, origin/main) add .tfvars
4053270 change acm
4116ce5 WIP
b16c53a WIP
e053901 add
0cb94c0 WIP
502e3c2 WIP
720e046 terraform fmt
c493b2a WIP
80740af WIP
e483178 WIP
4110d64 WIP
aac01ea WIP
b4eb7df WIP
1609422 meeting agenda
4538053 e-mail用のVPCエンドポイント追加
dffa447 IPアドレスの制限追加
13937d9 補足説明追加
fd988b2 リファクタリング”
dc5c551 WIP
054806d WIP
c79e2bd WIP
15bb453 WIP
a8c17ce WIP
ce8ec53 WIP
8bb2839 WIP
bf2762d WIP
d91cb2f WIP
ea4432d add
3c5f1c6 WIP
9561e31 アカウント共通であるセキュリティ関連を追加
070b6e3 WIP IAMの重複リソース回避
1778509 一通り動かすようにした。IAMを再確認
fd54eae WIP
f68ad9b WIP
977269e WIP
539698b add terraform files
5ba90c1 WIP add terraform env
6ec9946 WIP
3b29eea WIP
52be819 WIP
a1a66cc WIP
50e435e WIP
6a899a8 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
708e164 first commit

失敗

全部squashしてしまい、first commitの時点に戻ってしまいました。

それに気づかず、さらにcommitするという失態をおかしました。

失敗のリカバリ  コミットを無かったことにする

いったんコミットをもとに戻します。

git reset --soft HEAD^

で、確認します。そうすると、以下の状態です。

git reflog
708e164 (HEAD) HEAD@{0}: reset: moving to HEAD^
6757ec6 HEAD@{1}: commit: 構築時点
708e164 (HEAD) HEAD@{2}: rebase (start): checkout 708e164
905c97d (origin/main, main) HEAD@{3}: commit: add .tfvars
4053270 HEAD@{4}: commit: change acm
4116ce5 HEAD@{5}: commit: WIP
b16c53a HEAD@{6}: commit: WIP
e053901 HEAD@{7}: commit: add
0cb94c0 HEAD@{8}: commit: WIP
502e3c2 HEAD@{9}: commit: WIP
720e046 HEAD@{10}: commit: terraform fmt
c493b2a HEAD@{11}: commit: WIP
80740af HEAD@{12}: commit: WIP
e483178 HEAD@{13}: commit: WIP
4110d64 HEAD@{14}: commit: WIP
aac01ea HEAD@{15}: commit: WIP
b4eb7df HEAD@{16}: commit: WIP
1609422 HEAD@{17}: commit: meeting agenda
4538053 HEAD@{18}: commit: e-mail用のVPCエンドポイント追加
dffa447 HEAD@{19}: commit: IPアドレスの制限追加
13937d9 HEAD@{20}: commit: 補足説明追加
fd988b2 HEAD@{21}: commit: リファクタリング”
dc5c551 HEAD@{22}: commit: WIP
054806d HEAD@{23}: commit: WIP
c79e2bd HEAD@{24}: commit: WIP
15bb453 HEAD@{25}: commit: WIP
a8c17ce HEAD@{26}: commit: WIP
ce8ec53 HEAD@{27}: commit: WIP
8bb2839 HEAD@{28}: commit: WIP
bf2762d HEAD@{29}: commit: WIP
d91cb2f HEAD@{30}: commit: WIP
ea4432d HEAD@{31}: commit: add
3c5f1c6 HEAD@{32}: commit: WIP
9561e31 HEAD@{33}: commit: アカウント共通であるセキュリティ関連を追加
070b6e3 HEAD@{34}: commit: WIP IAMの重複リソース回避
1778509 HEAD@{35}: commit: 一通り動かすようにした。IAMを再確認
fd54eae HEAD@{36}: commit: WIP
f68ad9b HEAD@{37}: commit: WIP
977269e HEAD@{38}: commit: WIP
539698b HEAD@{39}: commit: add terraform files
5ba90c1 HEAD@{40}: commit: WIP add terraform env
6ec9946 HEAD@{41}: commit: WIP
3b29eea HEAD@{42}: commit: WIP
52be819 HEAD@{43}: commit: WIP
a1a66cc HEAD@{44}: commit: WIP
50e435e HEAD@{45}: commit: WIP
6a899a8 HEAD@{46}: commit: 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
708e164 (HEAD) HEAD@{47}: Branch: renamed refs/heads/master to refs/heads/main
708e164 (HEAD) HEAD@{49}: commit (initial): first commit

失敗のリカバリ rebase前の状態に戻したい

rebase前の905c97d (origin/main, main) HEAD@{3}: commit: add .tfvarsに戻したいです。

で、以下のコマンドを実施。HEAD@の数字がポイントです。

git reset --hard HEAD@{3}

そうすると、以下のようになる。

git reflog
905c97d (HEAD, origin/main, main) HEAD@{0}: reset: moving to HEAD@{3}
708e164 HEAD@{1}: reset: moving to HEAD^
6757ec6 HEAD@{2}: commit: 構築時点
708e164 HEAD@{3}: rebase (start): checkout 708e164
905c97d (HEAD, origin/main, main) HEAD@{4}: commit: add .tfvars
4053270 HEAD@{5}: commit: change acm
4116ce5 HEAD@{6}: commit: WIP
b16c53a HEAD@{7}: commit: WIP
e053901 HEAD@{8}: commit: add
0cb94c0 HEAD@{9}: commit: WIP
502e3c2 HEAD@{10}: commit: WIP
720e046 HEAD@{11}: commit: terraform fmt
c493b2a HEAD@{12}: commit: WIP
80740af HEAD@{13}: commit: WIP
e483178 HEAD@{14}: commit: WIP
4110d64 HEAD@{15}: commit: WIP
aac01ea HEAD@{16}: commit: WIP
b4eb7df HEAD@{17}: commit: WIP
1609422 HEAD@{18}: commit: meeting agenda
4538053 HEAD@{19}: commit: e-mail用のVPCエンドポイント追加
dffa447 HEAD@{20}: commit: IPアドレスの制限追加
13937d9 HEAD@{21}: commit: 補足説明追加
fd988b2 HEAD@{22}: commit: リファクタリング”
dc5c551 HEAD@{23}: commit: WIP
054806d HEAD@{24}: commit: WIP
c79e2bd HEAD@{25}: commit: WIP
15bb453 HEAD@{26}: commit: WIP
a8c17ce HEAD@{27}: commit: WIP
ce8ec53 HEAD@{28}: commit: WIP
8bb2839 HEAD@{29}: commit: WIP
bf2762d HEAD@{30}: commit: WIP
d91cb2f HEAD@{31}: commit: WIP
ea4432d HEAD@{32}: commit: add
3c5f1c6 HEAD@{33}: commit: WIP
9561e31 HEAD@{34}: commit: アカウント共通であるセキュリティ関連を追加
070b6e3 HEAD@{35}: commit: WIP IAMの重複リソース回避
1778509 HEAD@{36}: commit: 一通り動かすようにした。IAMを再確認
fd54eae HEAD@{37}: commit: WIP
f68ad9b HEAD@{38}: commit: WIP
977269e HEAD@{39}: commit: WIP
539698b HEAD@{40}: commit: add terraform files
5ba90c1 HEAD@{41}: commit: WIP add terraform env
6ec9946 HEAD@{42}: commit: WIP
3b29eea HEAD@{43}: commit: WIP
52be819 HEAD@{44}: commit: WIP
a1a66cc HEAD@{45}: commit: WIP
50e435e HEAD@{46}: commit: WIP
6a899a8 HEAD@{47}: commit: 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
708e164 HEAD@{48}: Branch: renamed refs/heads/master to refs/heads/main
708e164 HEAD@{50}: commit (initial): first commit

rebaseをやろうとすると怒られるから、アボートした

もう1回やろうとすると、怒られます。

git rebase -i 708e164
fatal: It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
	git rebase (--continue | --abort | --skip)
If that is not the case, please
	rm -fr ".git/rebase-merge"
and run me again.  I am stopping in case you still have something
valuable there.

アボートすると、元に戻ります。

git rebase --abort

複数回同じことをやってしまった

何かを間違えていると思って、色々とやってみました。

やっぱり、わからない。同じことを何回もやっていたら、もう訳がわからなくなりました。

git reflog
905c97d (HEAD -> main, origin/main) HEAD@{0}: rebase (finish): returning to refs/heads/main
905c97d (HEAD -> main, origin/main) HEAD@{1}: rebase (start): checkout 4053270
905c97d (HEAD -> main, origin/main) HEAD@{2}: reset: moving to HEAD@{9}
905c97d (HEAD -> main, origin/main) HEAD@{3}: rebase (abort): updating HEAD
708e164 HEAD@{4}: rebase (start): checkout 708e164
905c97d (HEAD -> main, origin/main) HEAD@{5}: rebase (abort): updating HEAD
708e164 HEAD@{6}: rebase (start): checkout 708e164
905c97d (HEAD -> main, origin/main) HEAD@{7}: rebase (abort): updating HEAD
905c97d (HEAD -> main, origin/main) HEAD@{8}: reset: moving to HEAD@{3}
708e164 HEAD@{9}: reset: moving to HEAD^
6757ec6 HEAD@{10}: commit: 構築時点
708e164 HEAD@{11}: rebase (start): checkout 708e164
905c97d (HEAD -> main, origin/main) HEAD@{12}: commit: add .tfvars
4053270 HEAD@{13}: commit: change acm
4116ce5 HEAD@{14}: commit: WIP
b16c53a HEAD@{15}: commit: WIP
e053901 HEAD@{16}: commit: add
0cb94c0 HEAD@{17}: commit: WIP
502e3c2 HEAD@{18}: commit: WIP
720e046 HEAD@{19}: commit: terraform fmt
c493b2a HEAD@{20}: commit: WIP
80740af HEAD@{21}: commit: WIP
e483178 HEAD@{22}: commit: WIP
4110d64 HEAD@{23}: commit: WIP
aac01ea HEAD@{24}: commit: WIP
b4eb7df HEAD@{25}: commit: WIP
1609422 HEAD@{26}: commit: meeting agenda
4538053 HEAD@{27}: commit: e-mail用のVPCエンドポイント追加
dffa447 HEAD@{28}: commit: IPアドレスの制限追加
13937d9 HEAD@{29}: commit: 補足説明追加
fd988b2 HEAD@{30}: commit: リファクタリング”
dc5c551 HEAD@{31}: commit: WIP
054806d HEAD@{32}: commit: WIP
c79e2bd HEAD@{33}: commit: WIP
15bb453 HEAD@{34}: commit: WIP
a8c17ce HEAD@{35}: commit: WIP
ce8ec53 HEAD@{36}: commit: WIP
8bb2839 HEAD@{37}: commit: WIP
bf2762d HEAD@{38}: commit: WIP
d91cb2f HEAD@{39}: commit: WIP
ea4432d HEAD@{40}: commit: add
3c5f1c6 HEAD@{41}: commit: WIP
9561e31 HEAD@{42}: commit: アカウント共通であるセキュリティ関連を追加
070b6e3 HEAD@{43}: commit: WIP IAMの重複リソース回避
1778509 HEAD@{44}: commit: 一通り動かすようにした。IAMを再確認
fd54eae HEAD@{45}: commit: WIP
f68ad9b HEAD@{46}: commit: WIP
977269e HEAD@{47}: commit: WIP
539698b HEAD@{48}: commit: add terraform files
5ba90c1 HEAD@{49}: commit: WIP add terraform env
6ec9946 HEAD@{50}: commit: WIP
3b29eea HEAD@{51}: commit: WIP
52be819 HEAD@{52}: commit: WIP
a1a66cc HEAD@{53}: commit: WIP
50e435e HEAD@{54}: commit: WIP
6a899a8 HEAD@{55}: commit: 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
708e164 HEAD@{56}: Branch: renamed refs/heads/master to refs/heads/main
708e164 HEAD@{58}: commit (initial): first commit

冷静になって調べる

ここで以下の記事を参考にしました。

コミットをまとめる方法(git rebase -i)

一番新しいのにまとめるというよりは、古いのに新しいのをまとめるという考えでした。

また、省略形を使えるので、fを使用しました。

以下は、git rebaseの編集画面です。一番上だけpickであとはfにしています。

pick 6a899a8 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
f 50e435e WIP
f a1a66cc WIP
f 52be819 WIP
f 3b29eea WIP
f 6ec9946 WIP
f 5ba90c1 WIP add terraform env
f 539698b add terraform files
f 977269e WIP
f f68ad9b WIP
f fd54eae WIP
f 1778509 一通り動かすようにした。IAMを再確認
f 070b6e3 WIP IAMの重複リソース回避
f 9561e31 アカウント共通であるセキュリティ関連を追加
f 3c5f1c6 WIP
f ea4432d add
f d91cb2f WIP
f bf2762d WIP
f 8bb2839 WIP
f ce8ec53 WIP
f a8c17ce WIP
f 15bb453 WIP
f c79e2bd WIP
f 054806d WIP
f dc5c551 WIP
f fd988b2 リファクタリング”
f 13937d9 補足説明追加
f dffa447 IPアドレスの制限追加
f 4538053 e-mail用のVPCエンドポイント追加
f 1609422 meeting agenda
f b4eb7df WIP
f aac01ea WIP
f 4110d64 WIP
f e483178 WIP
f 80740af WIP
f c493b2a WIP
f 720e046 terraform fmt
f 502e3c2 WIP
f 0cb94c0 WIP
f e053901 add
f b16c53a WIP
f 4116ce5 WIP
f 4053270 change acm
f 905c97d add .tfvars

確認してみます。ちゃんと2行にまとまってます。

git log --oneline
b857af2 (HEAD -> main) 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
708e164 first commit

コミットメッセージの変更

以下でコミットメッセージを変更します。

git commit --amend -m "構築完了でコミットまとめ"

以下でコミットメッセージが変更されたか確認します。

git log --oneline
086250f (HEAD -> main) 構築完了でコミットまとめ
708e164 first commit

リモートにPUSH

以下でPUSHします。ポイントは-fの強制PUSHです。

git push -f origin main

参考

リモートにPUSHした状態のgit reflogは以下です。commitをまとめたり、commitメッセージを変更したりといった履歴がちゃんと残ってますね。

git reflog
086250f (HEAD -> main, origin/main) HEAD@{0}: commit (amend): 構築完了でコミットまとめ
b857af2 HEAD@{1}: rebase (finish): returning to refs/heads/main
b857af2 HEAD@{2}: rebase (start): checkout 708e164
b857af2 HEAD@{3}: rebase (finish): returning to refs/heads/main
b857af2 HEAD@{4}: rebase (fixup): 項目立てと記述したい内容を埋めた。次は図を作って入れ込む
2399dbf HEAD@{5}: rebase (fixup): # This is a combination of 43 commits.
cbdcb2c HEAD@{6}: rebase (fixup): # This is a combination of 42 commits.
1cb423e HEAD@{7}: rebase (fixup): # This is a combination of 41 commits.
cdfdb62 HEAD@{8}: rebase (fixup): # This is a combination of 40 commits.
aa63b32 HEAD@{9}: rebase (fixup): # This is a combination of 39 commits.
1269ef5 HEAD@{10}: rebase (fixup): # This is a combination of 38 commits.
fe49b19 HEAD@{11}: rebase (fixup): # This is a combination of 37 commits.
6ae43de HEAD@{12}: rebase (fixup): # This is a combination of 36 commits.
efeca3e HEAD@{13}: rebase (fixup): # This is a combination of 35 commits.
08c61cb HEAD@{14}: rebase (fixup): # This is a combination of 34 commits.
87c0c31 HEAD@{15}: rebase (fixup): # This is a combination of 33 commits.
82018a4 HEAD@{16}: rebase (fixup): # This is a combination of 32 commits.
01f9f3d HEAD@{17}: rebase (fixup): # This is a combination of 31 commits.
ff4a93a HEAD@{18}: rebase (fixup): # This is a combination of 30 commits.
7502315 HEAD@{19}: rebase (fixup): # This is a combination of 29 commits.
2875c09 HEAD@{20}: rebase (fixup): # This is a combination of 28 commits.
7898c51 HEAD@{21}: rebase (fixup): # This is a combination of 27 commits.
3512a2e HEAD@{22}: rebase (fixup): # This is a combination of 26 commits.
023685f HEAD@{23}: rebase (fixup): # This is a combination of 25 commits.
a941e89 HEAD@{24}: rebase (fixup): # This is a combination of 24 commits.
10ae638 HEAD@{25}: rebase (fixup): # This is a combination of 23 commits.
47b6632 HEAD@{26}: rebase (fixup): # This is a combination of 22 commits.
c4c3462 HEAD@{27}: rebase (fixup): # This is a combination of 21 commits.
fe3c891 HEAD@{28}: rebase (fixup): # This is a combination of 20 commits.
afdb41b HEAD@{29}: rebase (fixup): # This is a combination of 19 commits.
3f1dc43 HEAD@{30}: rebase (fixup): # This is a combination of 18 commits.
9508cce HEAD@{31}: rebase (fixup): # This is a combination of 17 commits.
2bb7d1a HEAD@{32}: rebase (fixup): # This is a combination of 16 commits.
9b51adf HEAD@{33}: rebase (fixup): # This is a combination of 15 commits.
390e2ca HEAD@{34}: rebase (fixup): # This is a combination of 14 commits.
fcafc3e HEAD@{35}: rebase (fixup): # This is a combination of 13 commits.
c9ba07d HEAD@{36}: rebase (fixup): # This is a combination of 12 commits.
3a77909 HEAD@{37}: rebase (fixup): # This is a combination of 11 commits.
45ad35b HEAD@{38}: rebase (fixup): # This is a combination of 10 commits.
3def769 HEAD@{39}: rebase (fixup): # This is a combination of 9 commits.
46a22f6 HEAD@{40}: rebase (fixup): # This is a combination of 8 commits.
05f8d1c HEAD@{41}: rebase (fixup): # This is a combination of 7 commits.
8aea3b4 HEAD@{42}: rebase (fixup): # This is a combination of 6 commits.
be3ba08 HEAD@{43}: rebase (fixup): # This is a combination of 5 commits.
ffc9c70 HEAD@{44}: rebase (fixup): # This is a combination of 4 commits.
4086be0 HEAD@{45}: rebase (fixup): # This is a combination of 3 commits.
92a0455 HEAD@{46}: rebase (fixup): # This is a combination of 2 commits.
6a899a8 HEAD@{47}: rebase (start): checkout 708e164
905c97d HEAD@{48}: rebase (finish): returning to refs/heads/main
905c97d HEAD@{49}: rebase (start): checkout 4053270
905c97d HEAD@{50}: reset: moving to HEAD@{9}
905c97d HEAD@{51}: rebase (abort): updating HEAD
708e164 HEAD@{52}: rebase (start): checkout 708e164
905c97d HEAD@{53}: rebase (abort): updating HEAD
708e164 HEAD@{54}: rebase (start): checkout 708e164
905c97d HEAD@{55}: rebase (abort): updating HEAD
905c97d HEAD@{56}: reset: moving to HEAD@{3}
708e164 HEAD@{57}: reset: moving to HEAD^
6757ec6 HEAD@{58}: commit: 構築時点
708e164 HEAD@{59}: rebase (start): checkout 708e164
905c97d HEAD@{60}: commit: add .tfvars