This is actually an issue with your pager (probably less
these days).
Most terminal emulators offer the concept of an alternate screen. Opening your editor switches to this alternate screen; text displayed in this screen remains in this screen, and only in this screen. Exiting the editor switches back to the main screen, and the alternate screen text vanishes, so that you're back to your command-line session, without the editor's display cluttering things up. Which is fine if that's what you wanted, and makes some sense when using the editor.
Unfortunately, the implementation here is to do this switching for everything that uses cursor-addressing modes, and less
uses cursor-addressing modes. So this means that piping output through less
also switches to the alternate screen.
There are numerous work-arounds and fixes. The simplest for less
itself is to use the -X
option, as described in this bug report and the less
documentation:
-X or --no-init
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.
Note that Git defaults to running less -FRX
, so if you (a) are using less
and (b) are not getting -FRX
, check to see if you've overridden the defaults, through core.pager
(or $GIT_PAGER
) and/or through the environment variable LESS
.
Some users (including myself) really, really hate this alternate-screen switching and wish for our editor output to remain on the screen. Here, a more powerful trick is to disable the alternate screen entirely. This is harder, however. See, e.g., How can you turn off alternate screen in OSX's Terminal.app? Some people really, really like this behavior and want to turn it on when it's off: see, e.g., screen: how to turn on alternate screen? (which has more links to how to turn it off).
(I use the "decompile the terminfo, edit out the alt-screen escape sequences, and compile my own terminfo" method myself.)