38
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git logのformatをカスタマイズした時に桁あわせする。ついでにgit logのフォーマットいろいろ。

Last updated at Posted at 2014-04-30

概要

git logの出力のformatについて調べたのでまとめる。桁あわせの仕方だけ知りたい場合は「もっとカスタマイズしたいよ!」のところから読めばおk。

基本

普通にやったら誰もが知ってるこんな感じ

$ git --no-pager log -3
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
Author: takc923 <takc923@gmail.com>
Date:   Mon Nov 25 02:38:28 2013 +0900

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
Author: takc923 <takc923@gmail.com>
Date:   Sun Nov 24 20:55:16 2013 +0900

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
Author: takc923 <takc923@gmail.com>
Date:   Sat Nov 23 17:15:32 2013 +0900

    onload was nonsense...

で、基本的なフォーマットはgitさんが用意してくれてて、--pretty=FORMATオプションで指定する。指定できるフォーマットはoneline, short, medium, full, fuller, email, raw(--pretty=medium == オプションつけない時のフォーマット)
それぞれこんな感じ。

$ git --no-pager log -3 --pretty=medium
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
Author: takc923 <takc923@gmail.com>
Date:   Mon Nov 25 02:38:28 2013 +0900

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
Author: takc923 <takc923@gmail.com>
Date:   Sun Nov 24 20:55:16 2013 +0900

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
Author: takc923 <takc923@gmail.com>
Date:   Sat Nov 23 17:15:32 2013 +0900

    onload was nonsense...
$ git --no-pager log -3 --pretty=oneline
c410c7b649729020a6a3ceea97e42ff8cd6375cd release v0.1.3
9753d717ec2a37d42167148287229cd3a2f15c4d add webstore link into readme
281e4b81d062ab96d19b75a92ddccdcb3583d56f onload was nonsense...
$ git --no-pager log -3 --pretty=short
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
Author: takc923 <takc923@gmail.com>

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
Author: takc923 <takc923@gmail.com>

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
Author: takc923 <takc923@gmail.com>

    onload was nonsense...
$ git --no-pager log -3 --pretty=medium
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
Author: takc923 <takc923@gmail.com>
Date:   Mon Nov 25 02:38:28 2013 +0900

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
Author: takc923 <takc923@gmail.com>
Date:   Sun Nov 24 20:55:16 2013 +0900

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
Author: takc923 <takc923@gmail.com>
Date:   Sat Nov 23 17:15:32 2013 +0900

    onload was nonsense...
$ git --no-pager log -3 --pretty=full
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
Author: takc923 <takc923@gmail.com>
Commit: takc923 <takc923@gmail.com>

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
Author: takc923 <takc923@gmail.com>
Commit: takc923 <takc923@gmail.com>

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
Author: takc923 <takc923@gmail.com>
Commit: takc923 <takc923@gmail.com>

    onload was nonsense...
$ git --no-pager log -3 --pretty=fuller
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
Author:     takc923 <takc923@gmail.com>
AuthorDate: Mon Nov 25 02:38:28 2013 +0900
Commit:     takc923 <takc923@gmail.com>
CommitDate: Mon Nov 25 02:38:28 2013 +0900

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
Author:     takc923 <takc923@gmail.com>
AuthorDate: Sun Nov 24 20:55:16 2013 +0900
Commit:     takc923 <takc923@gmail.com>
CommitDate: Sun Nov 24 20:55:16 2013 +0900

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
Author:     takc923 <takc923@gmail.com>
AuthorDate: Sat Nov 23 17:15:32 2013 +0900
Commit:     takc923 <takc923@gmail.com>
CommitDate: Sat Nov 23 17:15:32 2013 +0900

    onload was nonsense...
$ git --no-pager log -3 --pretty=email
From c410c7b649729020a6a3ceea97e42ff8cd6375cd Mon Sep 17 00:00:00 2001
From: takc923 <takc923@gmail.com>
Date: Mon, 25 Nov 2013 02:38:28 +0900
Subject: [PATCH] release v0.1.3


From 9753d717ec2a37d42167148287229cd3a2f15c4d Mon Sep 17 00:00:00 2001
From: takc923 <takc923@gmail.com>
Date: Sun, 24 Nov 2013 20:55:16 +0900
Subject: [PATCH] add webstore link into readme


From 281e4b81d062ab96d19b75a92ddccdcb3583d56f Mon Sep 17 00:00:00 2001
From: takc923 <takc923@gmail.com>
Date: Sat, 23 Nov 2013 17:15:32 +0900
Subject: [PATCH] onload was nonsense...
$ git --no-pager log -3 --pretty=raw
commit c410c7b649729020a6a3ceea97e42ff8cd6375cd
tree 5fdd01636a221c8e980ae6d35afe59987df5a10b
parent 9753d717ec2a37d42167148287229cd3a2f15c4d
author takc923 <takc923@gmail.com> 1385314708 +0900
committer takc923 <takc923@gmail.com> 1385314708 +0900

    release v0.1.3

commit 9753d717ec2a37d42167148287229cd3a2f15c4d
tree e56b0f2118228ac8bdd7e23cb7f1a9ca5d1b08fb
parent 281e4b81d062ab96d19b75a92ddccdcb3583d56f
author takc923 <takc923@gmail.com> 1385294116 +0900
committer takc923 <takc923@gmail.com> 1385294116 +0900

    add webstore link into readme

commit 281e4b81d062ab96d19b75a92ddccdcb3583d56f
tree 0f38a3eb42bfdaa90df0b899c922ac74bf2a0134
parent dbbdca090ae49d92a06afa38f66592675466bee8
author takc923 <takc923@gmail.com> 1385194532 +0900
committer takc923 <takc923@gmail.com> 1385194532 +0900

    onload was nonsense...

あと、git configでデフォルトを指定することが出来る

$ git config format.pretty oneline
$ git --no-pager log -3
c410c7b649729020a6a3ceea97e42ff8cd6375cd release v0.1.3
9753d717ec2a37d42167148287229cd3a2f15c4d add webstore link into readme
281e4b81d062ab96d19b75a92ddccdcb3583d56f onload was nonsense...

あと、commit hashを短いやつにしたり、時間のformatを変えたり出来る。続きはgit help logCommit Formattingの項目で!

もっとカスタマイズしたいよ!

これで満足できないあなたに、更にカスタマイズするにはこんな感じで自分で指定できる

$ git --no-pager log -3 --pretty=format:'%s | %H | %cn'
release v0.1.3 | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
add webstore link into readme | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
onload was nonsense... | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

ここで使えるplaceholder(%始まりのあれ)はcommit hash、省略されたcommit hash、commiter name, auther date, etc... 要は表示したいやつは大体ある( git help log or http://git-scm.com/docs/git-logformat:<string> のところ参照)

で、上記のフォーマットだと最初に長さがバラバラなcommit messageが来てて見づらい。ので、桁あわせする(このエントリで本当に書きたかったのはこれ。他はついでに書いた。)
%<(30)%sとか書くと、30桁で %s (commit message) を桁あわせしてくれる(追記: v1.8.3以降で利用可能)。つまり

$ git --no-pager log -3 --pretty=format:'%<(30)%s | %H | %cn'
release v0.1.3                 | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
add webstore link into readme  | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
onload was nonsense...         | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

こんな感じになる。
で、例えばこれ桁数が小さいと

$ git --no-pager log -3 --pretty=format:'%<(10)%s | %H | %cn'
release v0.1.3 | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
add webstore link into readme | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
onload was nonsense... | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

やっぱりずれる。これを回避するために、桁数超えた場合に超えた分を消すことも出来る。%<(10,trunc)のように、桁数の後ろにカンマ区切りでtruncと書くと

$ git --no-pager log -3 --pretty=format:'%<(10,trunc)%s | %H | %cn'
release .. | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
add webs.. | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
onload w.. | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

こうなる。
で、右側じゃなくて左を消したり真ん中を消したりも出来る。truncじゃなくてltrunc, mtruncを指定すると

$ git --no-pager log -3 --pretty=format:'%<(10,ltrunc)%s | %H | %cn'
..e v0.1.3 | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
..o readme | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
..sense... | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923                                                                       

$ git --no-pager log -3 --pretty=format:'%<(10,mtrunc)%s | %H | %cn'
rele...1.3 | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
add ..adme | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
onlo..e... | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

これだと桁あわせする時左寄せだけど、右寄せにも出来る。%>(30)%sとか書くと

$ git --no-pager log -3 --pretty=format:'%>(30)%s | %H | %cn'
                release v0.1.3 | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
 add webstore link into readme | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
        onload was nonsense... | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

真ん中寄せにも出来る。%><(30)%sとか書くと

$ git --no-pager log -3 --pretty=format:'%><(30)%s | %H | %cn'
        release v0.1.3         | c410c7b649729020a6a3ceea97e42ff8cd6375cd | takc923
add webstore link into readme  | 9753d717ec2a37d42167148287229cd3a2f15c4d | takc923
    onload was nonsense...     | 281e4b81d062ab96d19b75a92ddccdcb3583d56f | takc923

こうなる。

終わりに

git help log見ると説明書いてるけど分かりづらいしぐぐってもなかなか出てこなくて桁あわせするだけでわりと時間食った。。。とりあえずgitのhelpにはここらへんの実例載せて欲しかった。

38
39
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?