165

I have a couple of git repositories that belong together, and simple batch/bash file to loop over them. I often loop over them with a log command to quickly see what state they are in. This works nicely, except for one thing: if the commit message is longer than the number of characters my console is wide (or has multiple lines), git shows the line, then a newline with (END) and I have to press q to continue (I guess it pipes the output through more or something like that). Example:

> gitloop . "git log --decorate=short --pretty=oneline -n1"
18629ae238e9d5832cb3535ec88274173337a501 (HEAD, origin/master, master) short log

625fb891b9b0b8648459b07ace662ae3b7773c7f (HEAD, origin/master, origin/HEAD, master) short log

dc0838118266ba8570ea338c1faddfe8af0387bb (HEAD, origin/work, origin/master, work, master) oops loooooooooooooong log
-(END)

This is rather inconvenient as I have to press q a couple of time, whereas I'd just like to see all those oneliners in one go.

How can I disable this behaviour (preferrably while still keeping this log format)?

stijn
  • 34,664
  • 13
  • 111
  • 163
  • 4
    possible duplicate of [why do I have to hit q at the end of git log](http://stackoverflow.com/questions/2364978/why-do-i-have-to-hit-q-at-the-end-of-git-log) – Sgoettschkes Dec 19 '12 at 09:13
  • Thanks, the SEO worked and I found this! My main use case is different: I want to scroll back in terminal and be able to see the commit hashes from where I was the *last* time I ran `git log`. For some reason the Page prevents this in iTerm. The answers here all worked. Great. – Dan Rosenstark Jan 08 '22 at 22:57

6 Answers6

254

Git has an option to disable the pager:

git --no-pager log --decorate=short --pretty=oneline -n1

If your pager cuts lines and you want to retain that behaviour, either pipe to cut...

git --no-pager log --decorate=short --pretty=oneline -n1 | cut -c 1-$COLUMNS

...or set the environment variable GIT_PAGER before the invocation:

GIT_PAGER="cut -c 1-${COLUMNS-80}" git log --decorate=short --pretty=oneline -n1
Sam Whited
  • 6,880
  • 2
  • 31
  • 37
toabi
  • 3,986
  • 1
  • 24
  • 24
  • 1
    Is there a way to make `--no-pager` the default or even better at a shortcut for this option when needed ? I thought of aliasing `gitnp` to `git --no-pager`, but I think there is a better solution. – Islam Azab May 25 '16 at 10:44
  • 1
    @IslamAzab, you could alias `git` to `/usr/bin/git --no-pager` which would disable the pager for all your git calls. – mrkmg Jul 03 '16 at 20:48
  • 3
    @WarrenP `git` does not have built in pager. Instead, it follows Unix philosophy and uses whatever you have in `$PAGER`. `git` simply always uses `$PAGER` instead of doing that only randomly unlike many other poorly written tools. The only magic `git` has is that if `$LESS` is *not defined*, it will define `$LESS` to value `FRX`. See around https://github.com/git/git/blob/master/Documentation/config.txt#L766 for more details. – Mikko Rantalainen Mar 27 '17 at 06:51
  • @WarrenP `git` simply provides an *override* `GIT_PAGER` that will allow you to define custom value to be used instead of `PAGER` whenever git needs paging. – Mikko Rantalainen Mar 27 '17 at 06:55
  • 2
    Simplest case that fixes the behavior just for git: export GIT_PAGER=cat – Stragulus Oct 05 '18 at 19:20
  • @IslamAzab `-P` is an alias for `--no-pager`, for example, `git -P log -1 --name-only`. – Tom Hundt Mar 17 '21 at 22:09
50

Another solution for the problem of permanently disabling pager specifically when using log subcommand:

  • for current repo only:
    git config pager.log false

  • for your git installation (i. e. all repos on your machine):
    git config --global pager.log false

As you can guess, the same works if pager is needed to be on or off for some other subcommands selectively.
E. g. for branch (which prints branches) subcommand it will be

git config pager.branch false


Proposed solution is arguably more elegant comparing to

  • using git --no-pager each time you run certain command.
    Because, quite possible, you don't want to type it each time.

  • specifying git --no-pager as an alias for git
    Because, quite possible, you want to avoid implicit global config OR you want pager to be enabled in some cases.

  • rely on some environment variables like PAGER or GIT_PAGER.
    Because to do that, you need to ensure they're set in your current terminal session. And, if you want them to be set to some custom value automatically each time your new terminal is created, you need to alter one of shell-bootstrapped files like e. g. ~/.bashrc. It's not a big problem. But these bootstrapped files frequently are altered by other applications as well and contain bunch of other stuff, not just that used by Git. So, in theory, it's better to specify git-related settings using git config rather than put them in e. g. ~/.bashrc.


The alternative solution for disabling pager for all subcommands is to specify cat as the utility git will use for paging:

  • git config core.pager cat OR
  • git config --global core.pager cat

My answer is somewhat rephrasing of the one below:
"prevent git diff from using a pager?"
https://stackoverflow.com/a/6986231/6103242

It's referenced to point out another relevant discussion.

vladZams
  • 737
  • 6
  • 9
  • Once I do this is it possible to do something like `git --pager log` when I actually want it from time to time? – Arsen May 10 '22 at 10:03
  • 1
    @Arsen I don't think it's directly supported. `git help log` page does not have any mention about `pager` as an option. What you can do though is to add custom wrapper-function in `~/.zshrc` or `~/.bashrc` file so it does 3 things: switches the pager ON, executes `git log`, switches the pager OFF. `gitLogWithPager() { git config pager.log true; git log ${@}; git config pager.log false; }` Reopen your terminal and then you should be able to use `gitLogWithPager` command. `${@}` forwards any additional arguments to `git log`. – vladZams May 11 '22 at 16:55
  • This is the most helpful answer, think it should be the accepted one – amy Jan 18 '23 at 22:08
7

Disable pager for all commands:

git config --global core.pager '' 
marchozaur
  • 135
  • 1
  • 3
  • 1
    This works great! On Windows + Powershell use (double quotes inside single quotes): git config --global core.pager '""' – Tolga Aug 05 '21 at 05:13
3

You pipe it to less -F in case --no-pager does not work for you.

git log --decorate --oneline -5 | less -F

-F means that less will behave like --no-pager if the output fits on one screen, but become a pager otherwise.

Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
Nick Constantine
  • 963
  • 9
  • 19
  • 4
    That still paginates, though. Piping it to `cat` would avoid that. – Tom Hundt Mar 17 '21 at 22:06
  • Piping to `cat` is ideal if you have a bash alias like `alias glo="git log --color=always --pretty=tformat:'%C(brightblue)%h %C(white)%as %C(cyan)%an %C(green)%d %C(reset)%s'"` – Chris Beck Jan 20 '23 at 16:14
2
export PAGER=cat

worked for me

davesave
  • 2,863
  • 1
  • 18
  • 13
1

Although the above answers are probably correct, I would like add one that suites me best. I recommend configure less instead of git by adding the following flags:

export LESS="${LESS:+$LESS }-X -F"
-X  Disables sending the termcap initialization and deinitialization strings to the terminal.
-F or --quit-if-one-screen -> no need to press `q` if output fits into the current terminal size.