749
votes

Is there a command line switch to pass to git diff and other commands that use the less pager by default?

I know I can pipe it to cat, but that removes all the syntax highlighting.

I know I can set the pager in the global .gitconfig to cat by GITPAGER=cat (or something like that); but I want to have pager sometimes (depending on the size of the diff).

But, I would prefer a command line switch if there is one; and I am not able to find one, going through the man pages.

18
Note: core.pager 'less -+F -+X' would be a more recent way to remove those options. See my answer below.VonC
"less -+F -+X" is the magical setting to remove the annoying "...skipping..." markers in long logs. Thanks a lot for those options, you saved my day!Guillaume Perrot
See related question on using less with git (it varies dependent on less version): unix.stackexchange.com/questions/107315/…Pierz
Looking for a solution on Windows? @Jaredcheeda's answer stackoverflow.com/a/57399465/8604951 works .Jarmos

18 Answers

812
votes

--no-pager to Git will tell it to not use a pager. Passing the option -F to less will tell it to not page if the output fits in a single screen.

Usage:

git --no-pager diff

Other options from the comments include:

# Set an evaporating environment variable to use 'cat' for your pager
GIT_PAGER=cat git diff

# Tells 'less' not to paginate if less than a page
export LESS="-F -X $LESS"
# ...then Git as usual
git diff
320
votes

As a previous answer mentioned, passing the -F option to less causes it to quit if the content is less than one screen. However, after doing so, the screen is reset, and you end up not seeing the content.

The -X option does away with that behaviour. So, I use the following to enable conditional paging based on the amount of content:

git config --global --replace-all core.pager "less -F -X"
202
votes

Use

git config --global core.pager cat

to get rid of a pager for all commands for all repositories.

You can also disable paging for single Git subcommands by using pager.<cmd> setting instead of core.pager, and you can change your settings per Git repository (omit --global).

See man git-config and search for pager.<cmd> for details.

73
votes

The recent changes in the documentation mention a different way of removing a default option for less ("default options" being FRSX).

For this question, this would be (git 1.8+)

git config --global --replace-all core.pager 'less -+F -+X'

For example, Dirk Bester suggests in the comments:

export LESS="$LESS -FRXK" 

so that I get colored diff with Ctrl-C quit from less.

Wilson F mentions in the comments and in his question that:

less supports horizontal scrolling, so when lines are chopped off, less disables quit-if-one-screen so that the user can still scroll the text to the left to see what was cut off.


Those modifications were already visible in git 1.8.x, as illustrated in "Always use the pager for git diff" (see the comments). But the documentation just got reworded (for git 1.8.5 or 1.9, Q4 2013).

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 FRSX
(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=FRSX less -+S. The environment tells the command to set the S option to chop long lines but the command line resets it to the default to fold long lines.


See commit 97d01f2a for the reason behind the new documentation wording:

config: rewrite core.pager documentation

The text mentions core.pager and GIT_PAGER without giving the overall picture of precedence. Borrow a better description from the git var(1) documentation.

The use of the mechanism to allow system-wide, global and per-repository configuration files is not limited to this particular variable. Remove it to clarify the paragraph.

Rewrite the part that explains how the environment variable LESS is set to Git's default value, and how to selectively customize it.


Note: commit b327583 (Matthieu Moy moy, April 2014, for git 2.0.x/2.1, Q3 2014) will remove the S by default:

pager: remove 'S' from $LESS by default

By default, Git used to set $LESS to -FRSX if $LESS was not set by the user.
The FRX flags actually make sense for Git (F and X because sometimes the output Git pipes to less is short, and R because Git pipes colored output).
The S flag (chop long lines), on the other hand, is not related to Git and is a matter of user preference. Git should not decide for the user to change LESS's default.

More specifically, the S flag harms users who review untrusted code within a pager, since a patch looking like:

-old code;
+new good code; [... lots of tabs ...] malicious code;

would appear identical to:

-old code;
+new good code;

Users who prefer the old behavior can still set the $LESS environment variable to -FRSX explicitly, or set core.pager to 'less -S'.

The documentation will read:

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.

73
votes

You can disable/enable pagers for specific outputs in the global configuration as well:

git config --global pager.diff false

Or to set the core.pager option, just provide an empty string:

git config --global core.pager ''

This is better in my opinion than setting it to cat as you say.

15
votes

This worked for me with Git version 2.1.4 on Linux:

git config --global --replace-all core.pager cat

This makes Git use cat instead of less. cat just dumps the output of diff to the screen without any paging.

12
votes

Regarding the disabled color when piping:

Use --color to avoid that coloring is disabled.

git diff --color | less -R

Or configure it forced on (in e.g. .gitconfig):

[color]
        ui = on

git diff | less -R

For non-color tools, then use:

git diff --no-color | some-primitive-tool

Exporting environment variable LESS=-R (in e.g. .bashrc) turns on color support by default in "less":

git diff | less

7
votes

I have this hunk in my .gitconfig and it seems to work fine (disabled for both diff and show):

[pager]
    diff = false
    show = false
7
votes

You can add an alias to diff with its own pager with pager.alias, like so:

[alias]
  dc = diff
  dsc = diff --staged
[pager]
  dc = cat
  dsc = cat

This will keep the color on and use 'cat' as the pager when invoked at 'git dc'.

Also, things not to do:

  • use --no-pager in your alias. Git (1.8.5.2, Apple Git-48) will complain that you are trying to modify the environment.
  • use a shell with !sh or !git. This will bypass the environment error, above, but it will reset your working directory (for the purposes of this command) to the top-level Git directory, so any references to a local file will not work if you are already in a subdirectory of your repository.
6
votes

I like to disable paging from time to time, when I know the output is not very long. For this, I found a neat trick using Git aliases:

git config --global --add alias.n '!git --no-pager'

Or add the following to the [alias] section of ~/.gitconfig:

n = !git --no-pager

This means that you can use the prefix n to turn off paging for any Git command, i.e.:

git n diff # Show the diff without pager
git n log -n 3 # Show the last three commits without pager
git n show v1.1 # Show information about a tag
5
votes

As it says on man git, you can use --no-pager on any command.

I use it on:

git --no-pager diff
git --no-pager log --oneline --graph --decorate --all -n 10

Then use an alias to avoid using (and remembering) long commands.

5
votes

If you use the oh-my-zsh, in the ~/.oh-my-zsh/lib/misc.zsh file, comment this line:

env_default 'LESS' '-R'
4
votes

For Windows it is:

git config --global core.pager ""

This will turn off paging for everything in git, including the super annoying one in git branch.

3
votes

By default git uses uses less as pager. I normally prefer more, as it will print the first page and then allow you to scroll through the content.

Further, the content will remain in the console when done. This is usually convenient, as you often want to do something with the content after lookup (eg. email the commiter and tell him he introduced a bug in his last commit).

If you then want to pipe content, it would be inconvenient to scroll to print everything. The good thing with more is you will be able to combine it with pipeline and it will pipe through everything, eg.

# Find the commit abcdef123 in the full commit history and read author and commit message
git log |grep -C 5 'abcdef123'

Basically more is all you would ever need, unless you do not want the content to remain in the console when done. To use more instead, do as below.

git config --global core.pager 'more'
3
votes

Just follow below instructions.

  1. Just type vi ~/.gitconfig in your terminal.
  2. Paste LESS="-F -X $LESS" line.
  3. Press :wq and enter.
  4. Restart your terminal.
2
votes

For a quick-and-dirty script I wrote, I did it this way:

PAGER=cat git diff ...
0
votes
git -P diff

Or --no-pager.

BTW: To preserve colour with cat

git diff --color=always | cat
0
votes

Related to this, if you have a default pager set, and you want to create an alias that uses a different pager, you can use the -c option to pass the configuration parameter to just one command.

For example, I have delta configured as my default git pager, but I can create a lessdiff git alias like this, for cases when I want to use less instead.

[alias]
    lessdiff = -c core.pager=less diff