【Git】コミットログを綺麗に保つための技術
はじめに
皆さんはgitのcommitログを綺麗に保っていますか。
私も以前まではcommitログを綺麗に保つことは意識せずに適当にommitをしてしまっていました。
しかしながら、コミットログを綺麗に保っていないと色糸な弊害がでてきます。
例えば、、、、
「コードのレビューをしたいけど、コミットが汚くて、どんな作業をしたものかが分かりづらい」
「バグが発生したので、バグが起きていない地点まで戻りたいんだけど、コミットログが汚くて、履歴を追うのが大変」
上記のような問題をなるべく減らすためにも、コミットログを綺麗に保つことは重要だと思います、
そこで、今回は私がよく利用しているコミットログを綺麗に保つためのコマンドやtipsについて紹介していきたいと思います。
直前にコミットの内容を修正する
直前のコミットの内容を修正するのはすごく簡単で、以下のコマンドを利用します。
$ git commit --amend
git stash
+ git rebase -i
)二つ以上前のコミットの内容を修正する
(手順
# 修正したいファイルを含めたファイル群を一旦退避させる
$ git stash
# 修正したいコミット地点に戻るための以下のコマンドを実行(コミットのハッシュ値は修正したいコミットの一つまえのものを指定)
$ git rebase -i [コミットのハッシュ値]
# 修正したいコミットのpick→editに変更する
# 変更ファイルをstashから取得する or ファイルの修正
$ git checkout stash@{[番号]} [ファイル名]
# コミットの内容を修正
$ git commit -amend
# rebaseの処理を完了する
$ git rebase --continue
テスト
テスト
以下のコミットログの場合を想定
README.md
ファイルを以下のcommitの修正に加える
$ git log --oneline
41df7a0 (HEAD -> main) test3.txtを追加
8304fba test2.txtを追加 #← の部分のcommitを修正したい
285d6cf test1.txtを追加
ab36e47 (origin/main, origin/HEAD) Initial commit
-
修正したいファイルを含めたファイル群を退避
Saved working directory and index state WIP on main: 41df7a0 test3.txtを追加
-
修正したいコミット地点に戻る
修正したいコミットの一つの前のハッシュ値を選択する。
今回の場合は
8304fba
を修正したいので、一つ前のコミットのハッシュ値(285d6cf
)を指定する。以下のコマンドを実行すると、設定しているeditorが開く(今回はvimが開く)
$ git rebase -i 285d6cf
-
修正したいコミットのpick→editに変更し保存する
edit 8304fba test2.txtを追加 pick 41df7a0 test3.txtを追加 # Rebase 285d6cf..41df7a0 onto 285d6cf (2 commands) # # Commands: ・・・・・・・
- 以下のコマンドを実行し、HEADが修正commitに移っていることを確認する
$ git log --oneline 8304fba (HEAD) test2.txtを追加 285d6cf test1.txtを追加 ab36e47 (origin/main, origin/HEAD) Initial commit
-
変更ファイルをstashから取得する or ファイルの修正
- stash listかを確認。
# stash listの確認 $ git stash list stash@{0}: WIP on main: 41df7a0 test3.txtを追加
- 変更ファイルをstash listから取得する
$ git checkout stash@{0} README.md Updated 1 path from aa6f530
- コミットの内容(message)を修正
$ git commit --amend
- rebaseを完了する
$ git rebase --continue # コミットのハッシュ値が変更されている事とHEADを確認する $ git log --oneline 1ffc7e2 (HEAD -> main) test3.txtを追加 93137f1 test2.txtを追加 285d6cf test1.txtを追加 ab36e47 (origin/main, origin/HEAD) Initial commit
- stash listかを確認。
git rebase -i
)複数のコミットを一つにまとめる。
(git rebase -i
コマンドのfixup
を利用することで複数のコミットを一つにまとめることができる。
fixupは直前のpickを指定したコミットに統合し、メッセージは破棄する
手順
# 1. 修正したいコミット地点に戻る
$ git rebase -i [コミットのハッシュ値]
# 2. 統合したいコミットをpick→fixup(f)に変更
# 3. 統合したいコミットの直前にcommit履歴を移動する
テスト
テスト
以下のコミットログの場合を想定
test4.txt
ファイルを以下のcommitの修正に加える
(5f49d9d
のコミットを285d6cf
のコミットに統合する)
5f49d9d (HEAD -> main) wip : test4.txtファイルを追加
1ffc7e2 test3.txtを追加
93137f1 test2.txtを追加
285d6cf test1.txtを追加 #←ここのコミットに一番最新のコミット統合する
ab36e47 (origin/main, origin/HEAD) Initial commit
-
統合するためのコミットを選択する
今回は285d6cf
に統合したいので、このコミットの一つ前のコミットのハッシュ値(ab36e47
)を選択する以下コマンドを実行すると、設定しているeditorが起動する(今回はvim)
$ git rebase -i ab36e47
-
統合したいコミット(統合元)をpick→fixup(f)に変更
pick 285d6cf test1.txtを追加 pick 93137f1 test2.txtを追加 pick 1ffc7e2 test3.txtを追加 fixup 5f49d9d wip : test4.txtファイルを追加 # Rebase ab36e47..5f49d9d onto 5f49d9d (4 commands) # # Commands:
-
統合したいコミット(統合先)の直前にcommit履歴を移動する
- 以下のように変更し、保存する
pick 285d6cf test1.txtを追加 fixup 5f49d9d wip : test4.txtファイルを追加 pick 93137f1 test2.txtを追加 pick 1ffc7e2 test3.txtを追加 # Rebase ab36e47..5f49d9d onto 5f49d9d (4 commands) # # Commands:
- 統合に成功したことを確認する
Successfully rebased and updated refs/heads/main.
$ git log --oneline $ git show 137c5ad --name-only /// Date: Tue Jun 29 23:11:04 2021 +0900 test1.txtを追加 test1.txt test4.txt
- 以下のように変更し、保存する
git rebase i
)二つ以上前のコミットメッセージを修正する
(git rebase -i
コマンドのreward
を利用することで複数のコミットを一つにまとめることができる。
rewardはコミットメッセージを変更する
手順
# 1.修正したいコミット地点に戻るための以下のコマンドを実行(コミットのハッシュ値は修正したいコミットの一つまえのものを指定)
$ git rebase -i [コミットのハッシュ値]
# 2. 修正したいコミットメッセージをpick→editに
テスト
テスト
以下のコミットログの場合を想定
test4.txt
ファイルを以下のcommitの修正に加える
(5f49d9d
のコミットを285d6cf
のコミットに統合する)
$ git log --oneline
64d6334 (HEAD -> main) fixup! test2.txtを追加
dcd3431 test3.txtを追加
ea65123 test2.txtを追加 #←ここのコミットメッセージを修正する
c7bdae9 test1.txtを追加
ab36e47 (origin/main, origin/HEAD) Initial commit
-
修正したいコミット地点に戻る
修正したいコミットの一つの前のハッシュ値を選択する。
今回の場合はea65123を修正したいので、一つ前のコミットのハッシュ値(c7bdae9)を指定する。
以下のコマンドを実行すると、設定しているeditorが開く(今回はvimが開く)
$ git rebase -i c7bdae9
-
修正したいコミットのpickをrewordに変更し、保存する
reword ea65123 test2.txtを追加 pick dcd3431 test3.txtを追加 pick 64d6334 fixup! test2.txtを追加 # Rebase c7bdae9..64d6334 onto c7bdae9 (3 commands) # # Commands:
-
保存するとコミットメッセージを入力する画面が表示されるので、コミットメッセージを修正し、保存する
test2.txtを追加(rewardを用いてコミットメッセージを修正) # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit.
-
コミットメッセージが修正されたことを確認する
$ git log --oneline e71545b (HEAD -> main) fixup! test2.txtを追加 bb4ba78 test3.txtを追加 25c02f1 test2.txtを追加(rewardを用いてコミットメッセージを修正) c7bdae9 test1.txtを追加 ab36e47 (origin/main, origin/HEAD) Initial commit
git reset --soft
)直前のコミットを取り消す(commitを取り消す)
(git reset --hard
コマンドを利用することで直前のコミットを削除することができる。
手順
$ git reset --soft HEAD^
テスト
テスト
以下のコミットログの場合を想定
直前のコミットe71545b
を取り消す。
$ git log --oneline
e71545b (HEAD -> main) fixup! test2.txtを追加
bb4ba78 test3.txtを追加
25c02f1 test2.txtを追加(rewardを用いてコミットメッセージを修正)
c7bdae9 test1.txtを追加
ab36e47 (origin/main, origin/HEAD) Initial commit
-
git resetで直前のコミット取り消し
$ git reset --soft HEAD^
-
取り消したコミットに含まれたファイル群を確認する
下記のように、commitされていたファイルがステージングにいることが確認できます。$ git status On branch main Your branch is ahead of 'origin/main' by 3 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: test5.txt
git reset --hard
)直前のコミットを削除(一つ前のcommitの状態に戻す)
(手順
$ git reset --hard HEAD^
テスト
テスト
(随時更新中です、)
Discussion