882

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.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
lprsd
  • 84,407
  • 47
  • 135
  • 168
  • 19
    Note: `core.pager 'less -+F -+X'` would be a more recent way to remove those options. See [my answer below](http://stackoverflow.com/a/18781512/6309). – VonC Sep 13 '13 at 08:19
  • 1
    "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 May 12 '14 at 09:04
  • related: https://stackoverflow.com/questions/12352049/always-use-the-pager-for-git-diff – Trevor Boyd Smith Mar 28 '19 at 13:06
  • See related question on using `less` with git (it varies dependent on `less` version): https://unix.stackexchange.com/questions/107315/less-quit-if-one-screen-without-no-init – Pierz Apr 28 '20 at 08:45
  • Looking for a solution on Windows? @Jaredcheeda's answer https://stackoverflow.com/a/57399465/8604951 works . – Jarmos Apr 30 '21 at 15:30

20 Answers20

988

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

# short option alternative
git -P diff
smathy
  • 26,283
  • 5
  • 48
  • 68
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I don't see why not. I have no idea how it would react if used for a predicate that doesn't need a pager at all though. – Ignacio Vazquez-Abrams Feb 02 '10 at 12:53
  • 49
    Or use GIT_PAGER or PAGER environment variables, or `core.pager` git config variable. – Jakub Narębski Feb 02 '10 at 14:43
  • 96
    Keep in mind that you must add --add-pager *before* your command, as in "git --no-pager log --format=blah" – Ana Betts Aug 15 '11 at 22:53
  • 7
    Also, if your terminal clears the screen after exiting less you'll want to add `-E` to your `less` options to make `-F` usable. – mgalgs Nov 08 '11 at 18:18
  • the `--pager` option is not available to the `git shortlog` command – Ernesto Jun 07 '13 at 13:53
  • @mgalgs much of the point of using less is so it only uses the secondary screen, rather than scrolling useful context off the screen. I.e. -E is an anti-feature much of the time. – Barry Kelly Jul 18 '13 at 12:59
  • @barry actually I don't use it that way anymore. But it works for some people (like myself two years ago). – mgalgs Jul 18 '13 at 16:52
  • 15
    Note that if you want to include `--no-pager` in an alias, you have to prefix the command with it, and to avoid an error, you have to make an alias like this: `git config alias.foo '!git --no-pager foo'`. Somewhat confusing. Simply aliasing to `'--no-pager foo'` won't work. – Jim Stewart Oct 10 '13 at 20:20
  • See Joseph Cheek's [answer](http://stackoverflow.com/a/23224608/2562319) below for reasons *not* to use --no-pager in an alias, along with info on the pager.alias system recommended for use instead of that. – jbyler Apr 11 '16 at 20:32
  • 1
    I don't get how this has been here for so long without anyone mentioning that you should use double quotes in all shells I'm aware of to expand the variables. On an unrelated topic, who makes these rules: *"Edits must be at least 6 characters. Is there something else to improve in this post?"* – Călin Mar 14 '18 at 20:15
  • To remove the paging: git config --global core.pager '' ... or use the paging git config --global core.pager 'more' ... git config --global core.pager 'less' – WoodyDRN Apr 09 '19 at 13:13
  • Just a note, in case you're using a command that doesn't accept the `--no-pager` flag (such as `git show`). You can simply pipe the command through the linux command `cat` to stdout. Example: `git show HEAD | cat -`. EDIT: Oops, just read the rest of OP's question and saw this was already known... carry on! – J.M. Janzen Jul 04 '19 at 22:13
  • 2
    @JimStewart I found `git config alias.foo '-c core.pager=cat foo'` to work just fine. But as you point out, it complains with a straight `--no-pager foo`. – Michael Mar 09 '20 at 20:48
  • 2
    `--no-pager` can also be shortened to `-P` – Alex Jasmin Sep 21 '20 at 17:59
  • @JimStewart is `!` -reason subcommand- needed here? – Timo May 19 '21 at 19:39
  • Should I put it in the `.bashrc`: `LESS="-F -X $LESS"`. How would I use it? Is LESS a var or an executable or both? – Timo May 20 '21 at 18:23
  • Note that some old versions of git used to allow `git diff --no-pager ...` but the correct syntax is `git --no-pager diff ...`. – Mikko Rantalainen Oct 06 '21 at 14:31
  • For the first part/half: Is it a permanent (configuration) change? Global? Per repository? Can you make it clearer in your answer? – Peter Mortensen Nov 25 '21 at 18:57
  • `LESS="-F $LESS"` was exactly what I needed - thanks!!! – lebowski Jan 31 '22 at 20:54
375

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ishaaq
  • 6,329
  • 3
  • 16
  • 28
  • 52
    Note: I had to use `git config --global --add core.pager "less -F -X"` in git `1.8.0.2`, the one above didn't work. – Dogbert Jan 31 '13 at 07:23
  • 12
    Great! Also to make it behave so not only with git but all programs add something like `export LESS="-RFX"` to your `.bashrc` or `.zshrc` – defhlt Oct 13 '14 at 19:07
  • Thank you for this; I've been plagued by an option in which git would truncate the first line from the output, and then get out of sync such that going down one line and then up one line would cause the 1st line of the correct to occupy the top, followed by the 3rd line from the correct output. Anyway, specifying this option resolved that issue. So bizarre. – Tim Harper Jan 26 '15 at 20:40
  • If using Git For Windows (which uses less built by Greenwood Software), the parameter you need is -E (source: http://www.greenwoodsoftware.com/less/faq.html#dashe ) – Gavin Ward Nov 09 '15 at 11:09
  • git specific option without requiring to change the functionality of `less` for the entire shell session. My final command was `git config --global core.pager "less -FXR"`. – Felipe Alvarez Dec 08 '17 at 01:37
  • Thanks! No idea why, but I already had this desired behaviour using Bash on Mac until I switched to Zsh. No less alias or gitconfig line existed. Anyway this answer fixed it. – fazy May 15 '18 at 16:34
  • It seems `-X` is not needed in my case at least (preferred since then output from commands like `git log` gets cleared after closing the pager). I'm using `-FKR`. – dhardy Jun 28 '18 at 13:24
  • 1
    Yes, as I explained in my answer, I prefer the screen not to be reset before and after the pager starts and exits, especially if it all fits in one screen, which is exactly what `-X` does. Don't use it if you don't like that. I like it because this way I can refer to things like git SHAs by looking at the result of previous git commands in my history without having to type the command again. Each to his/her own. – ishaaq Jun 28 '18 at 17:42
  • 5
    As of December 2017 if you are using version 530 of less or higher, you do not need `-X`, [here is a post from unix SE explaining this](https://unix.stackexchange.com/a/107355/121116). `less -F` will produce the desired behavior. – Captain Man Apr 02 '20 at 15:40
  • Well, this still sucks. I would like it to avoid pager when fits the screen, but use pager and reset if it doesn't. Right now with -+X it works well when it doesn't fit screen and resets it after quit, but then if it fits the screen the output is not being seen - which is ridiculous. – Kamil Dziedzic Jan 18 '21 at 22:08
  • 1
    I think reset shouldn't apply if pager is not being used, and should apply otherwise. – Kamil Dziedzic Jan 18 '21 at 22:10
236

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
geekQ
  • 29,027
  • 11
  • 62
  • 58
  • The question was not how to disable pager altogether but how to disable it for the grep subcommand. mtahmed's answer is perfect. – TilmanBaumann Oct 06 '14 at 12:46
  • 15
    This might be what the extended explanation asked but this is what I was looking for and it was one of the top Google results so thanks @geekQ – Brian F Leighty Dec 30 '15 at 13:57
  • This would be terrible if you did `git log` in a repository with a large number of commits. Or worse, `git log -p` -- you'd get the entire set of commit diffs dumped into your terminal. – Ether Oct 15 '21 at 19:53
94

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mmtauqir
  • 8,499
  • 9
  • 34
  • 42
  • This is the one that worked for me `git version 2.5.4 (Apple Git-61)` – Ashley Coolman Feb 18 '16 at 14:47
  • 1
    This is what i wanted because other commands disable the pager completely, but this is the only answer that disables only for `git diff` while leaving it working for other commands – SingleNegationElimination Dec 09 '17 at 21:44
  • as of this writing ... on Windows / PowerShell I had to manually edit `code "$($env:UserProfile)\.gitconfig"` and add `pager = ` under the `[core]` settings. The answer worked for my current powershell session but didn't seem to save the setting anywhere so it was effective on the next launch. – No Refunds No Returns Oct 25 '18 at 14:40
  • 1
    To selectively page other options, use `git config --global pager.log "less -F -X"` or similar – Clucking Turtle Sep 22 '20 at 04:57
  • This is best option when/if you're using similar .gitconfig for Windows and Linux; though git-for-windows do have `cat` and `less`, it may not have `more`; disabling and/or forcing to nothing is more portable to multi-platform without guessing and/or bothering to ask "are you Linux or Windows?" – HidekiAI Nov 30 '20 at 15:24
  • NB: on Windows, using single quotes in the last command doesn't work, but double quotes do: `git config --global core.pager ""` – wovano Jul 06 '21 at 08:17
  • Worked great on git version 2.39.2 – Bruno Schaatsbergen Jun 22 '23 at 11:32
79

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.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @CoreDumpError note: the next Git (2.0.X or 2.1) won't chop line **by default**! See my edited answer above. – VonC Jul 27 '14 at 06:46
  • 2
    Thanks. This let me figure out that I want export LESS="$LESS -FRXK" so that I get colored diff with ctrl-c quit from less. – Dirk Bester Jan 27 '15 at 03:14
  • @DirkBester Interesting suggestion. I have included it in the answer for more visibility. – VonC Jan 27 '15 at 06:53
  • Here's a puzzle (well, it is to me): when I set `less` to quit for <1 screen and _not_ chop lines (i.e. `less -F -+S`), I get returned to my command prompt after I run the log command. However, if I _do_ have it chop lines (i.e. remove `-+S`), _and any lines get chopped_, then when it ends, it _doesn't_ quit immediately, but prints `END` and waits for me to press `q`. Is there a way to chop lines and still exit from `less` automatically after less than a screen? – Wilson F Sep 22 '15 at 19:09
  • 1
    @WilsonF I don't know, but that sounds like a good question: why not make it a question instead of leaving it here, buried in comments? – VonC Sep 22 '15 at 19:26
  • Yes, I should have. So, I did, thanks! http://unix.stackexchange.com/questions/231427/is-there-a-way-for-less-to-truncate-lines-and-still-exit-after-1-screen – Wilson F Sep 22 '15 at 21:23
  • For anyone who finds this and is interested but can't be bothered to follow the link ;-) : it turns out that `less` supports horizontal scrolling (maybe everyone knows this, but I didn't), 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. – Wilson F Sep 22 '15 at 23:59
20

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jcoffland
  • 5,238
  • 38
  • 43
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted.) – Nathan Tuggy Apr 19 '15 at 01:20
  • 5
    @NathanTuggy I thought it was pretty clear that I was answering the question asked. Seems like some users like to run around flagging short answers for fun. I'll add some text. – jcoffland May 13 '15 at 23:53
12

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

StellarVortex
  • 576
  • 4
  • 19
  • 2
    Note that git will set the `LESS` environment variable to `FRSX` when calling the pager, if it is unset. – tobbez Sep 27 '12 at 15:42
11

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
10

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joseph Cheek
  • 554
  • 5
  • 11
  • 1
    +1 This technique is useful when you want to navigate by commit after `git log` but not `git log --oneline`. Use `pager.log = less -FXR +/^commit.*`, while special-casing the `--oneline` option with `alias.l1 = log --oneline` and `pager.l1 = less -FXR`. – Claudio Oct 03 '18 at 11:54
8

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.

Is Ma
  • 913
  • 11
  • 14
8

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

[pager]
    diff = false
    show = false
sekmo
  • 1,517
  • 18
  • 27
5

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

env_default 'LESS' '-R'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
slow-rain
  • 61
  • 1
  • 1
  • Hey, I think the OP wanted to have a command line switch to use single time with git diff, rather than a permanent global solution. Also, I don't know how good it would be to modify oh-my-zsh source code, since it would be hard to update oh-my-zsh later on. Better set that default value on your .zshrc file. – Karri Rasinmäki Apr 16 '19 at 15:18
  • 1
    Thanks,you are right. If use the oh-my-zsh and need a permanent global solution, can add `unset LESS` in the end of the .zshrc. – slow-rain Apr 18 '19 at 14:23
  • 1
    Thanks for this. I found the best for me was to set the following in .zshrc - `export LESS="-F -E -X $LESS"` I prefer not to mess with the files from oh-my-zsh – olore Jan 05 '21 at 14:09
5

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'
patrik
  • 4,506
  • 6
  • 24
  • 48
  • 1
    if you get ugly output with a bunch of highlighted ESC blocks at beginning and ending of lines then use these options to fix it back to color. worked great for me... git config --global core.pager "more -MR " and that is all you need to do – luckyguy73 Jun 21 '21 at 00:11
4

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.

Jaredcheeda
  • 1,657
  • 17
  • 15
4

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.
Nitish
  • 995
  • 7
  • 17
3

I'm surprised I didn't see this answer yet, but simply:

PAGER= git diff ...

Appendix A: I'm on macOS and using zsh, not sure if that changes anything.
Appendix B: As others have pointed out, this is not the best solution if you want to use this as part of a script or uncontrolled environment, where $GIT_PAGER might be set.

SplittyDev
  • 551
  • 1
  • 10
  • 19
2

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

PAGER=cat git diff ...
Roberto Bonvallet
  • 31,943
  • 5
  • 40
  • 57
1
git -P diff

Or --no-pager.

BTW: To preserve colour with cat

git diff --color=always | cat
hyperpallium
  • 571
  • 5
  • 18
1

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
Christian Long
  • 10,385
  • 6
  • 60
  • 58
  • How would you use the alias? I mean you do not define to which command the `-c` option is for. In which file do you declare it? – jarno Apr 08 '21 at 00:26
  • These lines go in your git [config file](https://git-scm.com/docs/git-config). Git knows that this alias config applies to `git` commands. You can edit the user's global git config like this: `git config --edit --global`, or you can set up system-wide or per-repo git configurations. I updated my answer to clarify that this is a git alias, not a bash alias. – Christian Long Apr 12 '21 at 15:39
1

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'
DEV Tiago França
  • 1,271
  • 9
  • 9