I have a git log alias that prints each commit as a single line. Since some people write far too long one-liners in the commit log, many commits wrap to a new line. How can
Late to the party, but these options will do it too:
$ git config --global core.pager 'less -S'
or (e.g.)
$ echo $LESS
-R
$ export LESS=-RS
Try this:
git log --pretty=oneline
Hope it helps.
It is not that clear in the documentation just which characters are needed but the following example cuts the subject line to 50 characters:
git log --oneline --format="%h %<(50,trunc)%s"
The format specification is %<
and the arguments for that need to be in parentheses. In this case, 50 chars and truncate the excess.
For instance, performing this on the msysGit repository yields:
C:\src\msysgit>git log -n 5 --format="%h [%<(12,trunc)%aN] [%<(12,trunc)%cN] %<(50,trunc)%s"
218ed04 [Sebastian ..] [Sebastian ..] Merge pull request #154 from csware/tortoisegitp..
8a920b9 [Sven Stric..] [Sven Stric..] Installer: Detect TortoiseGitPlink from Tortoise..
448e125 [dscho ] [dscho ] Merge pull request #152 from csware/syscommand
db8d1bf [Sven Stric..] [Sven Stric..] Perl readline creates empty sys$command files if..
753d3d6 [Johannes S..] [Johannes S..] Git for Windows 1.8.5.2-preview20131230
I put this into ~/bin/git-mylog
:
#!/bin/bash
COLS=$(tput cols)
git log --format="tformat:%>|(15)%C(auto)%h %Cgreen %<(20,trunc)%cn %C(auto) %<(15,trunc)%ar %<($((COLS-55)),trunc)%s" --graph
There's a bit going on here, but the guts of it is to use tput cols
to get the terminal width and then do some arithmetic to truncate the comment width to the actual space available rather than a fixed width.
As per other answers, the format placeholders %<(50,trunc)%s
will print the commit message truncated at 50 characters. But that will also pad shorter values to the same, and there's no way to tell it not to.
If that suits you, then you're done. If not, another approach is needed.
Also per other answers, you could configure less -S
as the core.pager
option globally or per-repository. This will trim the entire log string at the terminal width, avoiding wrapped lines.
But it will do that to all Git commands! (At least all the ones that produce paged output).
You can do this with the -c
option, e.g. git -c core.pager='less -S' log --graph --oneline
Even better, set this up as an alias so you don't have to type it every time:
git config --global alias.graph "-c core.pager='less -S' \
log --graph --oneline`
You can also combine this with formatting placeholders. Here's an example using the --graph
flag, where the commit message is also padded/truncated to 50 characters, but since the --graph
option creates a variable-width drawing of the commit graph, you need to combine both approaches. And you don't want to be typing this out every time:
git config --global alias.graph "-c core.pager='less -S' \
log --pretty='tformat:%C(bold cyan)%h %C(blue)%<(10,trunc)%aN \
%<(50,trunc)%C(white)%s %C(auto)%d %C(dim green)%ar' --graph"
(I seem completely unable to format a comment appropriately so have posted this as an answer, but really is a comment to @patthoyts's response.)
What's lovely about trunc
is that it pads, so you can use it like so:
git log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse
to produce an easier (at least for my eyes) overview.
$ git log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse
2015-06-15 initial commit 5099ede
2015-06-16 Layout - Responsive grid added. 6534242
2015-06-17 HTML - H1 / Title updated <title>Testpage</title.. 88ea464
2015-06-18 Updating the Hotfix changes a8fbc47
Tip - add an alias of, say, trunc to make it easy on yourself.
git config --global alias.trunc 'log --pretty=format:"%ad %<(50,trunc)%s %h"
--date=short --reverse'