`less` performs differently when invoked from Bash and from Git

gitless

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?

Best Answer

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.

Related Question