terminalgit-logless-unix

How to make git-log scroll up instead of down


Whenever I view a git log --all --graph --oneline --decorate output in my terminal emulator, the first commit is viewed at the top of the terminal screen. When I quit the git log output view with q, a few lines from the are not visible any more, as there are some new lines appended to the bottom of the screen, for the next command.

Usually though, those top lines are the most interesting, as they resemble the most recent git history, so I want them to be still visible when I type the next git command.

How can I make the git log output appear starting at the bottom of the screen, i.e. such that the first commit is viewed at the bottom? You would have to scroll up to view older commits.

NOTE: The --reverse flag is not an option for two reasons.

  1. Each time you have to scroll all the way to the bottom to view the first commits. That should not be necessary. I want to start at the bottom.
  2. It doesn't combine with the --graph flag: fatal: cannot combine --reverse with --graph.

Solution

  • This is an answer that seems to catch most edge cases. Not tested thoroughly.

    [alias]
        rlog = !"git --no-pager log --all --graph --decorate --oneline --color=always | tac | awk -f ~/switchslashes.awk | less -X +G -r"
    

    where the file ~/switchslashes.awk contains

    {
        match($0,/([[:space:][:cntrl:]|*\/\\]+)(.*)/,a) # find the segment of the graph
        tgt = substr($0,RSTART,RLENGTH)     # save that segment in a variable tgt
        gsub(/\//,RS,tgt)                   # change all /s to newlines in tgt
        gsub(/\\/,"/",tgt)                  # change all \s to /s in tgt
        gsub(RS,"\\",tgt)                   # change all newlines to \s in tgt
        gsub(/_/,"¯",tgt)                   # change all _ to ¯ in tgt
        print tgt substr($0,RSTART+RLENGTH) # print tgt plus rest of the line
    }
    

    which is a modified version of this script. It replaces underscores with overlines and swaps slashes with backslashes and vice versa. This fixes the graph after the text has been reversed by tac.

    Disclaimer

    I never started using this, since it is slow with larger repositories. It needs to load everything and then apply the substitutions, which take too much time for my taste.