Search code examples
gitformattinggit-show

git show: formatting is changed after redirection to file


I run git show --stat 2a340b71b280d60bbc29f8d8fe00b7da5760e2bb. I get:

commit 2a340b71b280d60bbc29f8d8fe00b7da5760e2bb
Author: xxx <[email protected]>
Date:   Thu Apr 30 13:40:54 2020 +0200

    xxx

 xxx.c                                |    1 +

I run git show --stat 2a340b71b280d60bbc29f8d8fe00b7da5760e2bb > git_show.txt. I get:

$ cat git_show.txt
commit 2a340b71b280d60bbc29f8d8fe00b7da5760e2bb
Author: xxx <[email protected]>
Date:   Thu Apr 30 13:40:54 2020 +0200

    xxx

 xxx.c       |    1 +

I.e. the formatting (spacing) is different! Very unexpected.

The problem is that in the git_show.txt there are also .../xxx/xxx.c | 1 + entries with clipped filenames, which prevents to see the full filename.

Why the formatting change happens and how to preserve the formatting?

P.S. git version 2.8.0


Solution

  • The reason is that when you print the text to standard output, Git knows what the size of your terminal is in columns, and generates a width based on that value. When you pipe to a file, Git doesn't have your terminal attached to standard output, and therefore it defaults to 80 columns.

    If you want to use a different width, you can specify it as an argument to --stat:

    $ git show --stat=132 2a340b71b280d60bbc29f8d8fe00b7da5760e2bb > git_show.txt
    # or, if your shell supports $COLUMNS:
    $ git show --stat=$COLUMNS 2a340b71b280d60bbc29f8d8fe00b7da5760e2bb > git_show.txt
    

    There are additional parameters you can provide; for more information, see git-diff(1). Note that these options may not be available in 2.8.0; if that's the case, then you'll need to upgrade to get support for this feature.