Breaking News

Default Placeholder Default Placeholder

はじめに

昨今、開発作業ではgitを使うことが多いですよね。プロジェクトが進むにつれて、コミットはどんどん増えていきます。

コミット履歴を見たい、となった場合、多くの人は git logを使ってログを追ったりするかと思います。

しかし、、、多すぎる!!コミットが多すぎていつ何をしたかを探すのが大変!!!そんなコミット探し地獄の負担を少しでも減らしたい、と思い本記事を書きました。

本記事では、git logのオプション指定でこんなことができるんだ、というのをまとめていきます。

筆者の環境

  • git version 2.32.0 (Apple Git-132)

前提

  • コミットハッシュなどなど、コミット情報はぼかしています

通常のgit log

通常のノーオプションのgit logだと、出力はこんな感じですね。

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Mon May 4 13:51:38 2020 +0900

    skill chart

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Mon May 4 09:29:26 2020 +0900

    modified products

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sun May 3 14:10:23 2020 +0900

    added products

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sun May 3 11:09:16 2020 +0900

    first commit

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sat May 2 14:36:08 2020 +0900

ここからわかるのは

  • いつ
  • 誰が
  • コミットメッセージ

くらいしかわからないですね。

これを少しわかりやすくしていきましょう!

コミットの内容を見る

コミットの内容を見るには -pを付与します

$ git log -p

commit xxxxxxx (HEAD -> master, origin/master)
Author: thisisauthor <this.is.author@gmail.com>
Date:   Mon Sep 13 19:03:46 2021 +0900

    add new component

diff --git a/src/components/Products.vue b/src/components/Products.vue
index dbdd1f38..6b6c369a 100644
--- a/src/components/Products.vue
+++ b/src/components/Products.vue
@@ -7,6 +7,7 @@
   aaa-component
   bbb-component
   ccc-component
+  ddd-component
 </template>

このように、差分を表示することができます。

しかし、これだとどれだけスクロール量が多くなって結局探したいものが見つからないですよね。

そこで、git logの中身を検索します。

コミットメッセージで検索をかける

コミットメッセージで検索するには --grepを使います。

git log --grep=検索ワード

例えば、addというワードで検索してみます。

$ git log --grep=add

commit xxxxxxx (HEAD -> master, origin/master)
Author: thisisauthor <this.is.author@gmail.com>
Date:   Mon Sep 13 19:03:46 2021 +0900

    add file

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sun Oct 25 13:56:19 2020 +0900

    add max age

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sun Oct 25 13:14:01 2020 +0900

    added cloudfront invalidation command

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sun Oct 25 13:08:31 2020 +0900

    added exponential mark

コミットメッセージに addを含むものだけが出力されました。

いや、これでもまだ絞り込めない、という方もいるかと思います。

そこで今度は、コミットした日時も検索条件に入れてみましょう。

コミット日時を指定して検索する

コミット日時を指定するにはこんな感じにします。

$ git log --grep=add --since '2020/10/25 13:30' --until '2021/09/30 20:00'

commit xxxxxxx (HEAD -> master, origin/master)
Author: thisisauthor <this.is.author@gmail.com>
Date:   Mon Sep 13 19:03:46 2021 +0900

    add file

commit xxxxxxx
Author: thisisauthor <this.is.author@gmail.com>
Date:   Sun Oct 25 13:56:19 2020 +0900

    add max age

さっきより減りましたね。時間の指定はしなくてもいい、という方は

git log --grep=add --since '2020/10/25' --until '2021/09/30'

こんな感じでも検索できます。

スクロールするのが面倒だな、コミットを1行にまとめて欲しいな、という方は次のオプションを見てみましょう。

結果を1行にまとめる

git logの出力が多くて見にくいという人は、--oneline オプションを使ってコミットを1行にまとめて見れるようにしましょう。

$ git log --grep=add --since '2020/10/25' --oneline

xxxx (HEAD -> master, origin/master) add file
yyyy add max age
zzzz added cloudfront invalidation command
aaaa added exponential mark
bbbb added --exact-timestamps option

コミット内容は見えないですが、コミットメッセージで判断する場合はこの方が見やすいですね。