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.
- 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.
- It doesn't combine with the
--graph
flag:fatal: cannot combine --reverse with --graph
.
First of all you can alway pass
-n
to the log to print out any number of commits you are interested in.Use the
--reverse
flag:You can read here for more tips and flags regarding git log:
http://www.alexkras.com/19-git-tips-for-everyday-use/
Original answer, which doesn't work, so goto EDIT for the working version
The following
sed
solution works for me if used directly form command line. It does not use any temporary string to switch\
and/
by relying onsed
sy
command.It assumes the SHA code to be 7 characters long and uses it to "recognize" what is not the leading sequence of
\
,/
,|
,_
,*
, and<space>
, which I was not able to put at the beginning of the firsts
command's search pattern and in place of the first.
in the seconds
command.I don't know why I cannot get it to work when all
sed
commands are put in a script called in withsed
s-f
option.EDIT (The above code is actually faulty)
As @user1902689 pointed out,
sed
scripts can make anyone's eyes bleed, myself included, since they are extremely cryptic.In my opinion, the task would be easily accomplished if the
--color=always
was not used, in which case the text piped out ofgit log
would be the same as we see on screen; using--color=always
, on the contrary, inserts control sequences like^[[33m
interspersed in the text to control the coloring (different colors for different branches, ...).But having a colored output is nice, so I directed the output of
git log --color=always ...
to file, and looked into it, discovering that the hash always appears in between^[[33m
and^[[m
, where^[
is a single character obtainable by hitting Ctrl+V, then Esc. These are essentially the escape sequences interpreted by bash as setting the color to yellow, and back to white, respectively (link).The hash, which is not the only 7-alphanumeric characters-string in the line (e.g.
thiswrd
can be in the commit main message), is almost certainly the first one, so greedy expressions (sed
has no non-greedy expressions) can be safely used after it, and not before (a.*
before the hash-matching regexp would make that regexp match the last 7-alphanumeric characters-string on the line, which could beanytext
, for instance, and the hash would be lost somewhere in.*
). In order to allow the use of the greedy.*
in a way that it doesn't devour the hash, we can enclose the hash in between newlines\n
which are not matched by thewith an.
in.*
(and, as such, they must be explicitly typed)s
command, so that we can "limit" the greediness of.*
in a successives
command by using some\n
explicitly in the search pattern.I think the following code (explained afterward) is not definitive, since it hardcodes the coloring escape sequences used to get a colored hash string, but it works as long as I've tried.
Each line contains the three parts
Graph OpeningColorTagHashClosingColorTag Message
, or only the first one,Graph
. Thesed
string consists of 9 commands that do what I intended to do with the original answer, but in a bit different way (especially in that the order of some commands is inverted, to save ax
command).s
command puts a newline\n
on each side of theHash
string[0-9a-z]\{7\}
(or does nothing if there is no hash in this line; note that the lines before/after a merge/diverge have no hash or message following them). This has the purpose of "isolating"Hash
. Note that the capturing groups\(...\)
are numbered based on the order of occurrence of the opening token\(
, so in the replacement string\2\n\3\n\4\5
:†\2
refers to^[\[33m
, which is theOpeningColorTag
(NOTE:^[
is a single character obtained by hitting Ctrl+V, then Esc, whereas the "true"[
must be escped with a backslash\
);\3
refers toHash
,[0-9a-z]\{7\}
\4
refers to theClosingColorTag
,^[\[m
(what is said for\2
holds here too);\5
is whatever follows,.*
(implicitly up to end-of-line).Now the pattern space (the current line, as we've edited so far) contains the original line with two embedded newlines on each side of the hash (
Graph OpeningColorTag\nHash\nClosingColorTag Message
), or the unmodified original line if it contained no hash (Graph
).h
command "saves" the pattern space into the hold space (think of it as a drawer).Now the pattern and hold spaces have the same content (
Graph OpeningColorTag\nHash\nClosingColorTag Message
orGraph
).s
command captures and replace, with itself only (/\1/
) and discarding everything before it (^.*
, i.e.Graph OpeningColorTag
), either (\|
separates alternatives in a capturing group\(...\)
)\n[a-z0-9]\{7\}\n.*
,$
,Now the pattern space contains
\nHash\nClosingColorTag Message
, or the empty string if there was no hash.x
command exchanges the contents of pattern and hold spaces, so that the multiline\nHash\nClosingColorTag Message
(or the empty string) is saved in the hold space, and the multilineGraph OpeningColorTag\nHash\nClosingColorTag Message
is in the pattern space, ready to be re-edited.s
command command strips off the\nHash
, and everything following it, from the pattern space.Now the pattern space contains
Graph OpeningColorTag
.y
substitutes each character between the first two non-escaped/
, with the corresponing character between the second and third non-escaped/
. Here both back and forward slashes must be escaped with a backslash. (This shoul be safe, sinceOpeningColorTag
should not contain any of the translated characters.)Now the pattern space contains
Hparg OpeningColorTag
, whereHparg
is the "inverted" version ofGraph
(orHparg
only).G
command gets the content of the hold space and appends it to (hence the capitalG
; the lower caseg
would copy into instead of append to) the pattern space, with a newline\n
in between.Now the pattern space contains
Hparg OpeningColorTag\n\nHash\nClosingColorTag Message
(orHparg\n
only), and we don't care the hold space from now on.s
command captures the\nHash\n
part and substitutes it withHash
.Now the pattern space contains
Hparg OpeningColorTag\nHashClosingColorTag Message
orHparg\n
.s
command removes the remaining newline\n
.Finally the pattern space contains
Hparg OpeningColorTagHashClosingColorTag Message
orHparg
.Steps 8. and 9. cannot be fused together (e.g.
s/\n\n\([a-z0-9]\{7\}\)\n/\1/
), since the two\n
enclosing the hash are there only if the line contains the hash (the firsts
of point 1. does nothing if there's no hash), whereas the first\n
is always present, since it comes with theG
command.† Actually the most external group
\(
...\)
is not needed (it's not used, indeed), so it can be removed, and all numeric references to the other capturing groups can be diminished by 1, e.g.s/\(\(^[\[33m\)\([0-9a-z]\{7\}\)\(^[\[m\)\(.*\)\)/\2\n\3\n\4\5/
can be changed tos/\(^[\[33m\)\([0-9a-z]\{7\}\)\(^[\[m\)\(.*\)/\1\n\2\n\3\4/
; but I'll keep the unnecessary group in the answer, since it gives the chance to mention the not-so-widely-known numbering of capturing groups.A command that comes close to the intended result is
However, this still messes up the graph a little bit, as the slashes are not reversed properly.
Update
This command takes also care of swapping the slashes with backslashes and vice versa.
The corresponding git alias is
This is an answer that seems to catch most edge cases. Not tested thoroughly.
where the file
~/switchslashes.awk
containswhich 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.