Search code examples
gitgit-format

How to make git log cut long comments?


I have a git log alias that prints each commit as a single line. Since some people write far too long one-liners in the commit log, many commits wrap to a new line. How can I format the git log output to cut the comment after 50 characters?

I found this in the git-log man page but it will only pad short comments, not cut long ones.

%<(<N>[,trunc|ltrunc|mtrunc]): make the next placeholder take at least N columns,
  padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc),
  the middle (mtrunc) or the end (trunc) if the output is longer than N columns. Note that
  truncating only works correctly with N >= 2.

Solution

  • It is not that clear in the documentation just which characters are needed but the following example cuts the subject line to 50 characters:

    git log --oneline --format="%h %<(50,trunc)%s"
    

    The format specification is %< and the arguments for that need to be in parentheses. In this case, 50 chars and truncate the excess.

    For instance, performing this on the msysGit repository yields:

    C:\src\msysgit>git log -n 5 --format="%h [%<(12,trunc)%aN] [%<(12,trunc)%cN] %<(50,trunc)%s"
    
    218ed04 [Sebastian ..] [Sebastian ..] Merge pull request #154 from csware/tortoisegitp..
    8a920b9 [Sven Stric..] [Sven Stric..] Installer: Detect TortoiseGitPlink from Tortoise..
    448e125 [dscho       ] [dscho       ] Merge pull request #152 from csware/syscommand
    db8d1bf [Sven Stric..] [Sven Stric..] Perl readline creates empty sys$command files if..
    753d3d6 [Johannes S..] [Johannes S..] Git for Windows 1.8.5.2-preview20131230