I occasionally switch back and forth between coding in Visual Studio Code on my Windows laptop or MacBook. (VS Code, a code editor that works on Linux, Windows and macOS)
Everything works similarly between the two except when I run a ‘git log’ command. I’m unsure if I did any special setup with VS Code on the MacBook but the git log is so nice and concise. It gives me each commit all on one line and back-to-back with no dead white space: a shortened commit hash, the commit message, the author and relatively how long ago the commit was made.
On the other hand, the git log in VS Code on my Windows laptop gives me the whole shebang on multiple lines with extra space: a full commit hash, author name and email, full date and time and commit message.
The output is so long it seems to skip around and repeat itself. Error?
This is more info than I want when I just want a glimpse. There is probably a way to alter the git log directly but I’m going to experiment with Pretty Formats.
The documentation is still a little dense to me as I’m not familiar with all of the terminology but Pretty Formats has several built-in formats you can use as well as a multitude of ways to customize your log output.
There’s also some (dreaded) RegEx looking placeholders involved in this customization. Instead of piecing together my own string of placeholders and commands, I’m glad I found this blog by Chris Simpkins that provided me something to work with.
git log --graph --pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’ --abbrev-commit --date=relative
Breaking It Down
--graph — outputs the commits in a graph; this is probably more evident when there are multiple authors on a project
--pretty=format: — this is how to access all those customizable pretty formatting placeholders; which will all be in a string i.e. will be between ‘ ‘
%C — to specify a color
%h — abbreviated commit hash
%Creset — reset the color
%d — ref names; the pointer to the HEAD and origin/master
%s — subject; commit message (in the command above it’s not assigned a color = the text stays white)
%cr — committer date, relative
%an — author name
--abbrev-commit — abbreviates the commit
--date=relative — gives a relative date
You can play around with the above git log command to see the output out of curiosity and/or until you get it to how you want it to be. Rearrange it, delete placeholders, add placeholders, change the colors etc.
*note, you can also add extra characters/words
ex: Written by %an
ex: Committed %cr
ex: adding parentheses or brackets; (%cr) or <%an>
Colors I have found to work: black, white, red, yellow, green, blue, cyan, magenta
-note: sometimes the colors need to be put into parentheses in order to work
Additional Attributes — add these before the color attribute
ex: (bold blue)
ex: (reverse yellow) — acts like a highlighter where the area around the text is colored and the text remains black
ex: (brightred) — for some reason this only worked for me if I combined it with the color; makes the color lighter
I’ve found the format and colors that I like.
git log --graph --pretty=format:’%C(cyan)%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(magenta)<%an>%Creset’--abbrev-commit --date=relative
Now, I don’t want to type or paste this EVERY single time I want a pretty git log so there’s one final step.
Global Git Alias Configuration
I’m going to name/give this an alias of “pretty” but you can name yours whatever you’d like as long as you can remember it.
git config --global alias.pretty “log --graph --pretty=format:’%C(cyan)%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(magenta)<%an>%Creset’ --abbrev-commit --date=relative”
Now, I can easily summon the log output I want with the command
git pretty. (‘git log’ stays the same since we never altered it)