Last Updated: February 25, 2016
·
761
· speckins

Navigating git log/diff

Introduction

git users are certainly familiar with the log and diff subcommands for looking through relevant project commit information.

Quicker navigation

Assuming your pager is the default Unix stalwart, less, you can set up some aliases to more quickly more meaningfully navigate output from these commonly used commands. less has a "+" option to execute commands on startup, and git config accepts per-command pager settings. We'll use the combination of these to preload some searches that give us the power to quickly move through output from git.

# ~/.gitconfig
[alias]
    pl = log -p --stat
    df = diff
# ...
[pager]
    # automatically set up less to search for these based on the alias used
    pl = less -FX +'/^commit '
    df = less -FX +'/^diff '

Now you can use git pl to show the commit history along with the changes each commit introduced, and since the search pattern is preloaded with ^commit, you can jump from commit to commit using the n (next) and N (previous) less keys.

git df is similarly equipped with a preloaded search pattern, ^diff, which lets you navigate from file to file.

More less keys

Use spacebar to move forward a full page. The f and b keys will be familiar to vim users, moving you forward and backward a whole screen, respectively. Similarly g and G get you from the beginning or the end of the file in one shot.