61

I am using git on terminal on OS X 10 Yosemite.

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.

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

any advice will be appreciate this is very annoying... :(

4

11 Answers 11

71

git diff uses the same pager as less Unix command.

  • Use keys d and u to go down / up half pages (forward / backward technically)
  • Jump to last line: G
  • h for help
  • ! to input a command (eg. git add while scrolling)

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. Some examples:

git diff --staged > ~/patch
git show someCommitSHA > ~/patch
git diff main myBranch > ~/patch
git diff head '*pathWildcard*' > ~/patch

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 to show diff +/- 2 lines around some keyword

3
  • 1
    No need to create a patch and then open it in sublime. Just run git diff | subl Commented Sep 7, 2016 at 5:58
  • 3
    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 | 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. Interestingly, git help does not suffer from this problem.
    – mzabaluev
    Commented Jan 4, 2018 at 0:39
  • maybe there is no arrow back then but I would do down arrow to go down by line and shift down for more faster down scroll
    – buncis
    Commented Apr 4 at 21:00
35

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

Why does this work? Per the git-config manpage:

Note that git sets the LESS environment variable to FRSX if it is unset when it runs the pager.

Per the less manpage:

-X Disables sending the termcap initialization and deinitialization strings to the terminal. 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.

-F Causes less to automatically exit if the entire file can be displayed on the first screen.

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.

4
  • 2
    This needs to be upvoted more. Helped me out immensely
    – rix0rrr
    Commented Apr 26, 2021 at 12:47
  • Is there a way to have this and retain the behavior of just printing to the terminal for small outputs? I like this when using git diff or git show, but it's very annoying when using git branch -avv to open up a less instance with 10 lines rather than just printing. It prevents me from starting to type the next command and then copying the git branch name.
    – daboross
    Commented Jul 22, 2021 at 21:21
  • 1
    Posted an answer with my solution to that at stackoverflow.com/a/68491919/1907543. I was going to edit this one, but looking at the edit guidelines we're only supposed to fix formatting issues, not add whole new content. So - new answer it is!
    – daboross
    Commented Jul 22, 2021 at 21:55
  • 1
    Worked for me on ubuntu 22.04. Thank you!
    – intlsy
    Commented Feb 26, 2023 at 11:55
11

Per mzabaluev's comment:

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 | 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. Interestingly, git help does not suffer from this problem. – mzabaluev Jan 4 at 0:39

This magic also includes enabling page up (FN+up) and pagedown (FN+down).

A quick and dirty way around this is to invoke less yourself with:

git diff --color=always | less -r

The command line options retain git's coloured output. I got them from Can less retain colored output?

4
  • The problem with this approach is we lose the coloring, which helps a lot to interpret the code changes. Commented Jul 15, 2019 at 12:30
  • 1
    Ran into this with a new laptop, looked up config on an old laptop, git config --global core.pager "less -+\$LESS -RS" got it working with scrolling as I'd expect. Not entirely sure why. Commented Nov 21, 2019 at 20:51
  • @LeonardoRaele my solution keeps the colouring (on my machine at any rate)
    – MJ Walsh
    Commented Nov 22, 2019 at 17:32
  • 2
    @ognockocaten you should put that in as an answer, it's the best one so far.
    – MJ Walsh
    Commented Nov 22, 2019 at 17:33
7
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.

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.

Explanation

This is an extension to ognockocaten's answer.

That answer suggests:

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

This removes two options:

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

Causes less to automatically exit if the entire file can be displayed on the first screen.

and -X / --no-init:

-X Disables sending the termcap initialization and deinitialization strings to the terminal. 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. So, even for small commands like git diff, you'll get a full alternate screen.

Reading closely, it seems like re-adding -F would fix this. 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. This was fixed in version 530 (see 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.

So just re-adding -F, you'd get small outputs being completely erased, as if they were never printed. We can't re-add -X to fix this, as that breaks scrolling, but we can update to a newer version of 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.

5

You can use any of the less commands to navigate the history. See: frequently used less commands

Examples: In the bottom left of your terminal, you must be seeing something like this. enter image description here

In the above example, the history has 221 lines out of which the current window is showing lines 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

Skip x-number of lines: Type a number and hit enter.

Scroll down and scroll down Use Fn

Or use below aternatives.

Hit space bar. as ILI suggested.

Scroll up by a page

Hit 'w'

Go home

Fn Left-Arrow Go to end Fn Right-Arrow

5

Easiest way is to use less to read the diff output. You need to use the --color and -R options to keep the coloration.

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=R git diff
2

If you're using iTerm2, you can enable mouse / touchpad scrolling by doing the following:

  1. Open iTerm2
  2. In the menu bar, click iTerm2 -> Settings
  3. Click the Advanced tab
  4. Set the option "Scroll wheel sends arrow keys when in alternate screen mode" to Yes
1

Just push h in terminal with opened less after git diff - it call help window with shortcuts

screenshot with less help text

1
  • 3
    Hi Dmitry, consider copy/pasting code instead of images.
    – amiabl
    Commented Jun 17, 2020 at 15:30
1

Building up on @ognockocaten's answer, you can change the default options passed to the less command by changing the LESS environment variable.

To do that, add this line to your startup script. (probably ~/.bashrc, ~/.zshrc, or ~/.bash_profile)

export LESS="RS"

And it should work for any new terminal you open.

0

fn + option (⌥) + down

worked for me on mac

0

A solution that is up to date for macOS Sonoma, with no global tweaks (e.g. exporting LESS in all of your shells), and minimized impact on other default behavior:

git config --global core.pager 'less -+X'

In Sonoma, the newer version of less shipped with the OS includes the fix to the bug mentioned by daboross.

The only flag passed by Git that causes the problem is -X, others can be left up to Git to set as its developers see fit. For example, my installed version no longer sets -S, so the other recipes that also unconditionally set this flag might get different behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.