简体   繁体   English

从 mac 终端向下滚动 git diff

[英]scrolling down git diff from mac terminal

I am using git on terminal on OS X 10 Yosemite.我在 OS X 10 Yosemite 的终端上使用 git。

When I do git diff I get long list of changes and sometimes I just need to scroll to the end but I couldn't figure out a way to do that other than to hold the keydown on MacBook pro and then it scrolls slowly.当我执行 git diff 时,我会得到一长串更改,有时我只需要滚动到最后,但除了按住 MacBook Pro 上的 keydown 然后它慢慢滚动之外,我想不出办法做到这一点。

if I press FN+down key then it does not scroll unless I have made the scroll down journey already down using the down button, but does not go further down...如果我按下 FN+down 键,那么它不会滚动,除非我已经使用向下按钮进行了向下滚动,但不会向下滚动...

any advice will be appreciate this is very annoying... :(任何建议都会不胜感激,这很烦人...... :(

git diff uses the same pager as less Unix command. git diff使用与less Unix 命令相同的寻呼机。

  • Use keys d and u to go down / up half pages (forward / backward technically)使用键du向下/向上半页(技术上向前/向后)
  • Jump to last line: G跳到最后一行: G
  • Use h if you want to display the help如果要显示帮助,请使用h

Another trick is storing the diff as a patch file like they used to do in the email days!另一个技巧是将差异存储为补丁文件,就像他们过去在电子邮件时代所做的那样! Then you can open up the patch in any program (Sublime has syntax highlight red/green)然后你可以在任何程序中打开补丁(Sublime 有语法高亮红色/绿色)

Some examples:一些例子:

git diff master > ~/patch

git show someCommitSHA > ~/patch

git diff master myBranch -- *.js *.css > ~/patch patch of js & css diff from master git diff master myBranch -- *.js *.css > ~/patch patch of js & css diff from master


git apply ~/patch

Normally you would use the patch by applying the diff, but you can just open the file in any text editor.通常你会通过应用差异来使用补丁,但你可以在任何文本编辑器中打开文件。 It's useful if you don't want to make a full commit out of your diff, but still want to use it somewhere else or send it to a friend如果您不想完全提交差异,但仍想在其他地方使用它或将其发送给朋友,这很有用


You can also use git diff master | grep -C 2 someKeyword你也可以使用git diff master | grep -C 2 someKeyword git diff master | grep -C 2 someKeyword to show diff +/- 2 lines around some keyword git diff master | grep -C 2 someKeyword在某个关键字周围显示 diff +/- 2 行

git config --global core.pager "less -+\$LESS -RS"

On my new macOS laptop, that got the git log scroll working as I'd expect (page up / page down buttons working, color, wheel scroll).在我的新 macOS 笔记本电脑上,git log 滚动按我的预期工作(向上/向下翻页按钮工作、颜色、滚轮滚动)。

Why does this work?为什么这行得通? Per the git-config manpage :根据git-config 手册页

Note that git sets the LESS environment variable to FRSX if it is unset when it runs the pager.请注意,如果在运行寻呼机时未设置 LESS 环境变量,则 git 会将其设置为 FRSX。

Per the less manpage :根据较少的联机帮助页

-X Disables sending the termcap initialization and deinitialization strings to the terminal. -X 禁止将 termcap 初始化和去初始化字符串发送到终端。 This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.如果去初始化字符串做了一些不必要的事情,比如清除屏幕,这有时是可取的。

I don't actually know what any of that means, but that X breaks page up / page down / scroll wheel.我实际上不知道这意味着什么,但是 X 打破了向上翻页/向下翻页/滚轮。

-F Causes less to automatically exit if the entire file can be displayed on the first screen. -F 如果整个文件可以显示在第一个屏幕上,则让 less 自动退出。

If you don't have X, then F breaks when there's no paging, presumably because less exits immediately and something prevents it from printing directly to the terminal.如果你没有 X,那么 F 在没有分页时会中断,大概是因为 less 立即退出并且某些东西阻止它直接打印到终端。

Per mzabaluev's comment:根据 mzabaluev 的评论:

The OS X terminal has some magic integration with less which breaks when less is run as a subprocess by git: when run as git diff | OS X 终端与 less 有一些神奇的集成,当 less 作为 git 的子进程运行时会中断:when run as git diff | less, scrolling forward is possible with the touchpad gesture, but with the output of git diff it only scrolls the terminal output and cannot tell less to scroll forward.更少,可以使用触摸板手势向前滚动,但是使用 git diff 的输出它只会滚动终端输出并且不能告诉更少向前滚动。 Interestingly, git help does not suffer from this problem.有趣的是, git help 并没有遇到这个问题。 mzabaluev Jan 4 at 0:39 mzabaluev 1 月 4 日 0:39

This magic also includes enabling page up (FN+up) and pagedown (FN+down).这个魔法还包括启用向上翻页 (FN+up) 和向下翻页 (FN+down)。

A quick and dirty way around this is to invoke less yourself with:解决此问题的一种快速而肮脏的方法是通过以下方式调用less自己:

git diff --color=always | less -r

The command line options retain git's coloured output.命令行选项保留 git 的彩色输出。 I got them from Can less retain colored output?我从可以少保留彩色输出吗?

You can use any of the less commands to navigate the history.您可以使用任何 less 命令来浏览历史记录。 See: frequently used less commands 见:常用的less命令

Examples: In the bottom left of your terminal, you must be seeing something like this.示例:在终端的左下角,您一定会看到类似这样的内容。 在此处输入图像描述

In the above example, the history has 221 lines out of which the current window is showing lines 21-62.在上面的示例中,历史记录有 221 行,当前窗口显示第 21-62 行。

Go directly to a line number : You can directly go to a line by typing直接转到行号:您可以通过键入直接转到行

line-number (followed by) g   (no spaces necessary)

for example: to go to line#100 just type 100g例如:要转到第 100 行,只需输入100g

Skip x-number of lines: Type a number and hit enter.跳过 x 行:键入一个数字并按 Enter。

Scroll down and scroll down Use Fn向下滚动并向下滚动使用 Fn

Or use below aternatives.或使用以下替代品。

Hit space bar.按空格键。 as ILI suggested.正如 ILI 建议的那样。

Scroll up by a page向上滚动一页

Hit 'w'按“w”

Go home回家

Fn Left-Arrow Go to end Fn Right-Arrow Fn 左箭头转到结尾Fn 右箭头

Easiest way is to use less to read the diff output.最简单的方法是使用less来读取 diff 输出。 You need to use the --color and -R options to keep the coloration.您需要使用--color-R选项来保持着色。

git diff --color | less -R

This works fine for me.这对我来说很好。

You could make a macro for this command.你可以为这个命令制作一个宏。


Since I wrote this answer, I discovered that you can use the LESS environment variable to set options for less , so you can do the same as above even easier:自从我写了这个答案后,我发现您可以使用LESS环境变量来设置less的选项,因此您可以更轻松地执行与上述相同的操作:

LESS=R git diff
brew install less
git config --global core.pager "less -+\$LESS -RSF"

Installing a newer version of less fixes the bug where -F causes small outputs to entirely disapear without -X , and then the config command removes -X , which is what breaks scrolling.安装较新版本的 less 修复了-F导致小输出在没有-X的情况下完全消失的错误,然后 config 命令删除-X ,这是中断滚动的原因。

This will replace the less version MacOS ships with with the latest, but as they are the same utility (unlike GNU coreutils vs. BSD coreutils), this should not break things.这将用最新版本替换 MacOS 附带的less版本,但由于它们是相同的实用程序(与 GNU coreutils 与 BSD coreutils 不同),这不应该破坏事情。

Explanation解释

This is an extension to ognockocaten's answer .这是对ognockocaten 答案的扩展。

That answer suggests:该答案表明:

git config --global core.pager "less -+\$LESS -RS"

This removes two options:这删除了两个选项:

-F / --quit-if-one-screen : -F / --quit-if-one-screen

Causes less to automatically exit if the entire file can be displayed on the first screen.如果整个文件可以显示在第一个屏幕上,则导致 less 自动退出。

and -X / --no-init :-X / --no-init

-X Disables sending the termcap initialization and deinitialization strings to the terminal. -X 禁止将 termcap 初始化和去初始化字符串发送到终端。 This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen.如果去初始化字符串做了一些不必要的事情,比如清除屏幕,这有时是可取的。

This works for the question at hand, but is suboptimal, as less will always switch to an alternate page regardless of how small the output is.这适用于手头的问题,但不是最理想的,因为无论输出有多小, less总是会切换到备用页面。 So, even for small commands like git diff , you'll get a full alternate screen.所以,即使是像git diff这样的小命令,你也会得到一个完整的备用屏幕。

Reading closely, it seems like re-adding -F would fix this.仔细阅读,似乎重新添加-F可以解决这个问题。 However, a combination of two things causes this to break:然而,两件事的结合导致了这个问题:

  • on the version of less MacOS ships with, 487 , less will switch to the alternate screen first , before checking if the text is small enough, and thus print the output to the alternate screen buffer.在 MacOS 附带的 less 版本487上,less 将首先切换到备用屏幕,然后检查文本是否足够小,从而将输出打印到备用屏幕缓冲区。 This was fixed in version 530 (see https://unix.stackexchange.com/a/107355/39135 )这已在版本530中修复(请参阅https://unix.stackexchange.com/a/107355/39135
  • on MacOS, without -X , switching back from the alternate to main buffer clears the screen and thus erases all text that was just printed to the alternate screen buffer.在没有-X的 MacOS 上,从备用缓冲区切换回主缓冲区会清除屏幕,从而擦除刚刚打印到备用屏幕缓冲区的所有文本。

So just re-adding -F , you'd get small outputs being completely erased, as if they were never printed.因此,只需重新添加-F ,您就会得到完全擦除的小输出,就好像它们从未打印过一样。 We can't re-add -X to fix this, as that breaks scrolling, but we can update to a newer version of less !我们无法重新添加-X来解决这个问题,因为这会破坏滚动,但我们可以更新到更新版本的less

Thus it all comes together - we can use homebrew to install the latest stable less version, and then configure the pager to include the -F option, as per the command block at the start of this post.因此这一切都结合在一起了——我们可以使用自制软件安装最新的less版,然后根据本文开头的命令块配置寻呼机以包含-F选项。

Just push h in terminal with opened less after git diff - it call help window with shortcuts只需在git diff之后打开的终端中按下h - 它使用快捷方式调用帮助窗口

screenshot with less help text帮助文本较少的屏幕截图

Building up on @ognockocaten's answer, you can change the default options passed to the less command by changing the LESS environment variable.基于@ognockocaten 的答案,您可以通过更改LESS环境变量来更改传递给less命令的默认选项。

To do that, add this line to your startup script.为此,请将此行添加到您的启动脚本中。 (probably ~/.bashrc , ~/.zshrc , or ~/.bash_profile ) (可能是~/.bashrc~/.zshrc~/.bash_profile

export LESS="RS"

And it should work for any new terminal you open.它应该适用于您打开的任何新终端。

fn + option (⌥) + down fn + 选项 (⌥) + 向下

worked for me on mac在mac上为我工作

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

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