How do I prevent git diff from using a pager?

2019-01-02 16:39发布

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.

标签: git
11条回答
深知你不懂我心
2楼-- · 2019-01-02 16:54

You can disable/enable pagers for specific outputs in global config 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.

查看更多
美炸的是我
3楼-- · 2019-01-02 16:56

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

PAGER=cat git diff ...
查看更多
时光乱了年华
4楼-- · 2019-01-02 17:00

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 3 commits without pager
git n show v1.1 # Show information about a tag
查看更多
其实,你不懂
5楼-- · 2019-01-02 17:00

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.

查看更多
刘海飞了
6楼-- · 2019-01-02 17:03

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楼-- · 2019-01-02 17:06

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

# tell less not to paginate if less than a page
export LESS="-F -X $LESS"
# ...then git as usual
git diff
查看更多
登录 后发表回答