Search code examples
gitless-unix

Make git diff not to print to terminal


If I do less somefile.txt or man bash (which uses less internally), it displays the content, but after pressing Q, the content disappears.

git diff also uses less, but the output remains in console. How can I make git act the same as man?


Current behavior

me@mypc:~$ git diff
# I look at the diff and then press q. But it's all printed to the console
diff --git a/somefile.txt b/somefile.txt
...
...
...
me@mypc:~$
me@mypc:~$ less somefile.txt
# I look at the file content, but after I press q, console appears and nothing is printed there
me@mypc:~$

I realized that the git behavior is like less -X, so how can I force git to run less without that option?


Solution

  • This is the effect of less -F -X; see core.pager in git-config(1).

    From less(1):

       -F or --quit-if-one-screen
              Causes less to automatically exit if the entire file can be dis‐
              played on the first screen.
    
       -X or --no-init
              Disables sending the termcap initialization and deinitialization
              strings  to  the  terminal.   This is sometimes desirable if the
              deinitialization string does something unnecessary, like  clear‐
              ing the screen.
    

    Part of the "init" mentioned here involves switching to the alternate screen, so that the main screen is preserved.


    From git-config(1):

       core.pager
           Text viewer for use by Git commands (e.g., less). The value is
           meant to be interpreted by the shell. The order of preference is
           the $GIT_PAGER environment variable, then core.pager configuration,
           then $PAGER, and then the default chosen at compile time (usually
           less).
    
           When the LESS environment variable is unset, Git sets it to FRX (if
           LESS environment variable is set, Git does not change it at all).
           If you want to selectively override Git’s default setting for LESS,
           you can set core.pager to e.g.  less -S. This will be passed to the
           shell by Git, which will translate the final command to LESS=FRX
           less -S. The environment does not set the S option but the command
           line does, instructing less to truncate long lines. Similarly,
           setting core.pager to less -+F will deactivate the F option
           specified by the environment from the command-line, deactivating
           the "quit if one screen" behavior of less. One can specifically
           activate some flags for particular commands: for example, setting
           pager.blame to less -S enables line truncation only for git blame.
    
           Likewise, when the LV environment variable is unset, Git sets it to
           -c. You can override this setting by exporting LV with another
           value or setting core.pager to lv +c.