Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leave 'git diff' result in the terminal

Tags:

git

I use Git in the terminal. When I have to make some changes, I use git diff to see what I have changed. I'd like that output result to stay in the terminal, so I can review it after I press Q, since when I do press Q, the results all disappear.

like image 734
mygoare Avatar asked Dec 22 '12 15:12

mygoare


2 Answers

This happens because Git is outputting it through a pager. Instead, use:

git --no-pager diff

It is set to use a pager by the default configuration, and you can change this default to use cat instead to prevent you from having to type --no-pager with git config --global core.pager cat. You can read more in the documentation here.


Outputting to STDOUT and through a pager is much more complicated and requires tools beyond the scope of regular Unix redirection and pipes. You can redirect the output to STDERR with tee and pipe to less, which gives the illusion of you want. Note this is a hack and abuses the idea of STDERR

git diff | tee /dev/stderr | less

You may want to make this an alias if you intend on using it frequently.

like image 164
Trent Earl Avatar answered Oct 07 '22 21:10

Trent Earl


In general, I don't like less disappearing with the contents either. I typically set my LESS environment variable to eFRX:

export LESS=eFRX

It's the X that makes it stop taking the away the content. The F says to just exit if there is only one screens worth of content. The R helps to interpret ANSI color codes (you'll want that for git diff), and the X makes it stop clearing the screen before exiting.

It will make you happier with some other programs that use less too.

like image 20
John Szakmeister Avatar answered Oct 07 '22 22:10

John Szakmeister