简体   繁体   English

如何让 git log 不提示继续?

[英]How to make git log not prompt to continue?

I have a couple of git repositories that belong together, and simple batch/bash file to loop over them.我有几个属于一起的 git 存储库,以及简单的批处理/bash 文件来循环它们。 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).我经常用 log 命令循环它们以快速查看它们处于什么状态。这很好用,除了一件事:如果提交消息长于我的控制台宽(或多行)的字符数,git显示该行,然后是带有 (END) 的换行符,我必须按q继续(我猜它通过更多或类似的方式输出输出)。 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.这很不方便,因为我必须按q几次,而我只想一口气看到所有这些 oneliners。

How can I disable this behaviour (preferrably while still keeping this log format)?如何禁用此行为(最好仍保持此日志格式)?

Git has an option to disable the pager: Git 有一个禁用寻呼机的选项:

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 ...如果您的寻呼机切断线路并且您想保留该行为,请使用管道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

GIT_PAGER="cut -c 1-${COLUMNS-80}" git log --decorate=short --pretty=oneline -n1

Another solution for the problem of permanently disabling pager specifically when using log subcommand:使用log子命令时永久禁用寻呼机问题的另一种解决方案:

  • for current repo only:仅适用于当前回购:
    git config pager.log false

  • for your git installation (ie all repos on your machine):对于您的 git 安装(即您机器上的所有存储库):
    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对于branch (打印分支)子命令,它将是

git config pager.branch false


Proposed solution is arguably more elegant comparing to

  • using git --no-pager each time you run certain command.每次运行某些命令时使用git --no-pager
    Because, quite possible, you don't want to type it each time.因为,很有可能,您不想每次都输入它。

  • specifying git --no-pager as an alias for git指定git --no-pager作为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 .依赖一些环境变量,如PAGERGIT_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 eg ~/.bashrc .而且,如果您希望在每次创建新终端时自动将它们设置为某个自定义值,则需要更改 shell 引导文件之一,例如~/.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.但是这些引导文件经常被其他应用程序更改,并且包含大量其他内容,而不仅仅是 Git 使用的内容。 So, in theory, it's better to specify git-related settings using git config rather than put them in eg ~/.bashrc .因此,理论上,最好使用git config指定与 git 相关的设置,而不是将它们放在例如~/.bashrc中。


The alternative solution for disabling pager for all subcommands is to specify cat as the utility git will use for paging:所有子命令禁用pager器的替代解决方案是将cat指定为实用程序 git 将用于分页:

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

My answer is somewhat rephrasing of the one below:我的回答是对以下内容的某种改写:
"prevent git diff from using a pager?" “防止 git diff 使用寻呼机?”
https://stackoverflow.com/a/6986231/6103242 https://stackoverflow.com/a/6986231/6103242

It's referenced to point out another relevant discussion.它被引用以指出另一个相关的讨论。

禁用所有命令的寻呼机:

git config --global core.pager '' 

如果 --no-pager 对您不起作用,您可以将其通过管道传递给 less -F。

git log --decorate --oneline -5 | less -F
export PAGER=cat

为我工作

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:我建议通过添加以下标志来配置less而不是git

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM