Practical Git: Navigate git command pager output with Unix less commands

Trevor Miller
InstructorTrevor Miller

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

When using a git command that can have a large amount of output (like git log, git diff, or git blame), Git opens the command output in our terminal "pager"; on most modern Unix-based systems, the default pager will be "less". Learning a few less commands will help us deal with this git command output that opens in the pager. In this lesson, we show some of the most useful of the less commands: q (quit), j (down), k (up), Ctrl f (forward), Ctrl b (backward), /{search} (search), and n/N (next/previous search result).

This lesson only covers some of the most critical less commands; there are more commands available. A good chunk of the commands (and "motions") of the Unix pagers are also used by vi (or vim) and other Unix programs.

[00:00] In our command line, we're inside of a directory called "Utility Functions," which is a git repository. If we run a git command that has a lot of output like git log, it will open up in what's called a "Terminal Pager."

[00:13] We know that we're inside of the pager program because of the colon at the bottom of the screen. Unless you've changed the pager and use on your computer, you're most likely using the Unix less pager.

[00:23] Less has a lot of really useful commands that we can use to navigate around our pager. The most important one is q, which brings us back to our main command line prompt. That q stands for quit. After I run the q command, I usually clear my screen.

[00:38] Let's run git log again to get back into our pager output. We can use our down and up arrows to go up and down one line at a time. We can also use our PageUp and PageDown keys to go up and down one screenful at a time.

[00:52] We can do the same thing more efficiently using J and K to go down and up one line at a time. We can use Ctrl F as in forward to go forward a screenful, and Ctrl B as in backward to go backward a screenful.

[01:07] We can search for something specific by using the forward slash, which if you notice at the bottom of the screen changes our prompt to a forward slash. Then type in a search like Trevor. Once we hit Enter, it will highlight all of our search results. We can use the n key as in next to go to the next search result and capital N to go to the previous search result.

[01:30] For a quick review, I'm going to hit q to quit my pager output. When I run any git command that has a lot of output like git blame, diff, or log, it will open up in my pager. I can navigate by screens with Ctrl F and B, navigate by lines with J and K, search for a specific terms with the forward slash and the term, and then n and capital N to go forward and backward between those search results.

[01:57] Finally, q to quit and get back to my main prompt.

Frederic Rey
Frederic Rey
~ 6 years ago

So far I've been using git mostly on my own so I've never been confident working with others. A really well explained course, haven't finished yet but I've learned a lot already, thanks!

Trevor Miller
Trevor Millerinstructor
~ 6 years ago

Thank you Frederic!