Turning off the pager in git for the stash command only
Asked Answered
I

4

32

I generally like the use of the pager in git, but for git stash the pager annoys me. When calling git stash list, I don't want to be shown the three lines of output in the pager -- it forces me to press q just to make the output unavailable again when typing the folow-up git stash pop command.

One solution would be to use

git --no-pager stash list

but that's to much typing (I'm lazy). Following the man page of git config, I tried

git config --global pager.stash false

but this doesn't seem to do what the documentation says (actually, I didn't notice any effect). Then I tried

git config --global alias.stash "--no-pager stash"

again without any noticable effect.

The configuration gets properly updated, for example

git config pager.stash
false

It just does not have any effect. What am I missing? And how can I achieve that git stash does not use the pager?

Illstarred answered 19/11, 2010 at 13:17 Comment(5)
Do you have any other pager-related config? Stash doesn't use the pager for me.Starveling
Strike that: the list subcommand does indeed paginate, and ignores the config.Starveling
See my answer below: git config --global pager.stash false not working was a bug fixed in git 1.7.7.3 and later.Vernon
@sven-marnach could you change the right answer to cbowns's one?Lagena
Another motivation for same goal: I religiously run git stash show -v before git stash drop, and I want the diff to remain in terminal (in case I did a mistake, or just for reference — frequently I do want to redo a small part of the changes in a different way).Archipelago
V
50

As of 1.7.7.3, git config --global pager.stash false accomplishes this.

Vernon answered 17/11, 2011 at 19:10 Comment(0)
S
11

It looks like stash, and any other non-builtin command (written as a shell script, rather than in C) misses out on the pager config step. I sent a note to the git mailing list asking about this; it looks like it's a known issue, but not totally trivial to fix.

The primary reason you're seeing no effect from your alias is that git silently ignores aliases for built-in commands; the idea is that you never want to actually make a command inaccessible. For the alias to have a chance of being run, you need to name it something other than stash.

However, I believe that simple aliases are not permitted to affect the environment a git command is run in, which generally includes the options passed to git itself. If I use an alias like yours:

git config alias.foo --no-pager stash
git foo
fatal: alias 'foo' changes environment variables

If you want to do that properly, you'd have to use !git --no-pager stash, so that it'll spawn a subshell and reinvoke git.

Another temporary fix, since it's a shell script, would be to go edit libexec/git-core/git-stash directly. Just go find the list_stash function, and add the --no-pager option to its call to git log, or to cover the whole script, set GIT_PAGER=cat at the top.

Starveling answered 19/11, 2010 at 14:57 Comment(5)
Thanks for the info and for bringing this up on the git mailing list (I also read the answer to your mail there). Regarding the alias: I also tried a different name than stash, but the effect was the same. Strangely, I did not get an error message (version 1.7.2.3), so I figured the name wasn't the reason. I will just patch the script for now.Illstarred
@Sven: Do you actually have more than one screen's worth of stashes to list? Or is your pager not quitting after less than one screen of output?Starveling
@Sven: And I'm not sure why you don't get that error. That check was added a long, long time ago (v1.5.3). The subshell alias (!...) should work fine though too, though, and avoid having to edit git itself.Starveling
As I wrote in my original post, I usually have just a few stashes, and certainly never more than one page :). My pager (less of course) is not exiting anyway. I figured out this is because I set the LESS environment variable in my .bashrc, so git does not overwrite it with FRSX. I updated my .gitconfig to include -FX in the options to less manually, and now everything works fine.Illstarred
@Sven: Aha! I was wondering if you had a nonstandard pager or something. Glad you found a way!Starveling
C
8

Alternatively you can configure less to exit if there's less than one screen's worth of output:

export LESS='-F'

Or, verbosely:

export LESS='--quit-if-one-screen'

If you have colours in your git output, you'll probably also want to pass the -r flag:

export LESS='-F -r'
Constipate answered 26/3, 2012 at 9:19 Comment(4)
This solution is already buried in the comments to Jefromi's answer (somehow). It is good to have it in an answer of its own, though.Illstarred
Doesn't work here (xterm on pts) because after less quit, there is nothing left of less display, it redraws the previous prompts. But it works on the term "linux" on tty1.Zoroastrian
Yeah, on some systems/terminals output disappears because it all goes to "alternate screen" and is immediately switched back :-( Adding --no-init might help.Archipelago
you can also do git config --global core.pager 'less --quit-if-one-screen' if you prefer to affect only git and not other | less uses.Archipelago
P
0
stll = "!git --no-pager stash list"

is your friend.

Pendant answered 20/2, 2015 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.