Gitの小ネタおれおれAdvent Calendar 2022 – 19 日目
git log --pretty=xxx で表示する内容をカスタマイズできます。
git log --pretty=format:xxx だとさらに自由自在に変更できます。
--pretty=format:xxx
format の名の通りフォーマットを指定します。
とりあえず Hello World を。
$ git log --pretty=format:"Hello World!"
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
何もわからんですね。
コミットごとの情報は %x みたいなプレースホルダーから利用できます。
$ git log --pretty=format:"The author of %h was %an, %ar%nThe title was >>%s<<%n"
The author of 7f3301d361f was Joyce Er, 3 weeks ago
The title was >>Report cancellation errors in telemetry (#167799)<<
The author of f3996b416f8 was Joyce Er, 3 weeks ago
The title was >>Add 1-step Continue in Local Folder option (#167795)<<
The author of c87fa19f793 was Connor Peet, 3 weeks ago
The title was >>cli: avoid interactions when running integrated (#167780)<<
The author of 968026a46db was Raymond Zhao, 3 weeks ago
The title was >>Use includes instead of indexOf (#167778)<<
プレースホルダー一覧
使いそうなものだけ抜粋。
%%– “%” を出力。"\\"的な%ad– コミット作成日時 (Author Date)%an– コミット作者名 (Author Name)%ar– コミット作成日時(相対表示。X 日前、とか) (Author date (Relative))%b– コミットメッセージの本文%C(…)– 色の指定 (Color)%C(auto)– 自動着色%C(blue)– 青%C(green)– 緑%C(red)– 赤%C(reset)– 元の色%C(yellow)– 黄
%D– ブランチ名とかタグ名とか%H– コミット ID (Hash (?))%h– 短いコミット ID%n– 改行。\n的な。ちなみに\nはそのまま出力される%s– コミットの題 (Subject)
例
元の git log っぽいもの
$ git log --pretty=format:"%C(yellow)commit %H%C(auto)%d%nAuthor: %an <%ae>%nDate: %ad%n%n%s%n%n%b"
commit 7f3301d361f4805c404603dd4d76fcfd36347419 (HEAD -> main)
Author: Joyce Er <[email protected]>
Date: Wed Nov 30 22:19:51 2022 -0800
Report cancellation errors in telemetry (#167799)
commit f3996b416f847af62eef1a50c6c678dedd47a92e
Author: Joyce Er <[email protected]>
Date: Wed Nov 30 22:00:43 2022 -0800
インデントとかやりようがあるんかな。
コミットグラフ
--graph オプションでブランチの流れが見える線が付きます。これでどうマージされてるのかわかる。
$ git log --graph --date=short --pretty=format:"%C(yellow)%h %C(white)%s %C(green)%d %C(red)%cn %C(blue)%cd - %cr" 7e351937da8
* 7e351937da8 Don't use IObservableValue (#167072) GitHub 2022-11-23 - 4 weeks ago
* 765c46f7ca9 Merge pull request #167080 from microsoft/tyriar/166158 GitHub 2022-11-23 - 4 weeks ago
|\
| * 4cd7bfa2cdd Update xterm.js Daniel Imms 2022-11-23 - 4 weeks ago
* | 33671e4554a Merge pull request #165579 from rwe/shellint-no-decode-iterm2 GitHub 2022-11-23 - 4 weeks ago
|\ \
| * | 9eb2de787ab Merge branch 'main' into shellint-no-decode-iterm2 GitHub 2022-11-23 - 4 weeks ago
| |\|
その他
%an vs %cn
%an は Author Name 、%cn は Committer Name 。
試したところ committer の方は GitHub になったりするみたい。
$ git log --pretty=format:"%h Committer=%cn, Author=%an"
7f3301d361f Committer=GitHub, Author=Joyce Er
f3996b416f8 Committer=GitHub, Author=Joyce Er
c87fa19f793 Committer=GitHub, Author=Connor Peet
968026a46db Committer=GitHub, Author=Raymond Zhao
2a16bdb4677 Committer=GitHub, Author=Harald Kirschner
56d73a45e38 Committer=GitHub, Author=Peng Lyu
bffd0181d3f Committer=GitHub, Author=Raymond Zhao
c8dd73b410c Committer=GitHub, Author=Peng Lyu
cec4114c0c1 Committer=GitHub, Author=Matt Bierner
e84133953f2 Committer=GitHub, Author=Raymond Zhao
dfca1c7088b Committer=GitHub, Author=Raymond Zhao
おしまい
誰かの .gitconfig から alias をコピペしてくるのがおすすめ。