10

When I view a large diff with git diff, it gets paged with less. This is confirmed by opening another window and checking data from ps -aux and /proc.

However, when less is invoked by Git, it does not revert the terminal content to its previous state after hitting q (the diff content remains in terminal). But when I do

git diff commit1 commit2 --color | less -R

and quit less with a key q, the content disappears and the terminal reverts to the previous state.

More interestingly, if I do these

export PAGER=less LESS='-R'

and invoke git diff (or any other command that calls a pager), less behaves the same as if invoked directly from Bash shell.

i

Here's a brief screenshot describing my question. On the left pane the command are executed as following:

unset PAGER GIT_PAGER LESS
git diff HEAD^ HEAD

On the right pane you see the commands. The latest commit was 100+ lines of y written to a file. On both panes less is exited with key q.

Can anyone tell me what is different and explain why?

0

1 Answer 1

16

Documentation:

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).

The -X (--no-init) option is responsible for not clearing terminal after exit of less.

4
  • Wow, great answer! I'd like to know if there's a git config to change this behavior (change the LESS environment that Git sets).
    – iBug
    Sep 16, 2018 at 13:27
  • 2
    @iBug Looks like it is baked into the source. Thus you need to build your own Git to change that.
    – user311110
    Sep 16, 2018 at 13:51
  • 1
    I got Less 530 from FSF and compiled it myself. Now less -FR is very good to have. Thank you again!
    – iBug
    Sep 16, 2018 at 15:32
  • There is no need to recompile less. You can simply turn off the default git options. See the answer here: stackoverflow.com/questions/48511931/… What you want is: git config core.pager "less -+X -+F" (turns off -XF) To set this globally instead of for a single project add --global
    – Cole
    Dec 9, 2021 at 2:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .