How do I prevent 'git diff' from using a pager?

Viewed 181710

Is there a command line switch to pass to git diff and other commands that use the less pager by default? I know these methods exist:

  • git diff | cat... removes all syntax highlighting
  • git config --global core.pager cat sets the pager in the global .gitconfig to cat
  • export GIT_PAGER=cat

But I would prefer a command line switch.

19 Answers

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

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

[pager]
    diff = false
    show = false

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.

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

env_default 'LESS' '-R'

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'

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.

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.
git -P diff

Or --no-pager.

BTW: To preserve colour with cat

git diff --color=always | cat

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

The BEST pagination is less -R -F -X

# Global - all projects (if has no custom local pager)
git config --global core.pager '/usr/bin/less -R -F -X'

# Local - Only on project
git config core.pager '/usr/bin/less -R -F -X'
Related