By running the following from the command line, you can create a new Git command (git lg
).
git config --global alias.lg "log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"
This new command creates a much more useful log of Git commits.
I use Git a lot and by default git log
is not very helpful. Git log has a lot of optional parameters, which can make the output much more useful.
Examples
Default Git Log
Improved Git Log
Using a combination of command line parameters, the git log output becomes beautiful and infinitely more useful.
git log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'
Features of Improved Git Log
- each commit uses only one line (allowing a much deeper look into project history)
- commit IDs are displayed in abbreviated format
- branching is visually indicated
- colorized
- summary line of commit is displayed
- name of committer is displayed
How the Command Works
Much of the command is adding colors (see Git pretty formats colors). When the colors are stripped out the command becomes
git log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%h -%d %s (%ad) <%an>'
--color
enables colors--graph
draws a text-based graphical representation of the commit history on the left hand side of the output--date=format: '%Y-%m-%d %H:%M:%S'
defines the date format to use (e.g.2019-12-01 12:45:51
)--pretty=format:'%h -%d %s (%ad) <%an>'
A custom pretty format where we use the following placeholders:
git lg
While the Improved Git Log is amazing, typing out the list of parameters each time would be ridiculous. Fortunately, Git allows us to create our own aliases. In this case, we’re going to create an alias to git lg
. By default if you run git lg
, you’ll get the message
git: ‘lg’ is not a git command. See ‘git –help’.
However, using the command mentioned at the beginning of this article we can add the alias to our global configuration. Here it is again.
git config --global alias.lg "log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"
Now, every time we want the improved git log, we can type git lg
.
How the Alias Works
The git config --global
line above writes this new alias to your .gitconfig
file (which is found at ~/.gitconfig
on a Mac).
you forgot an ending double quote on your last code snippet.
Hi Billy,
Thank you for catching the missing closing double quote. I’ve now added it.
Thanks again.
Very nice, thanks!
Got this set up locally with some minor changes. I found you can use %C(auto)%d%Creset to restore the standard “decorate” colors.
BTW: The screenshot shows dates in relative format rather than %Y-%m-%d %H:%M:%S. I guess some other (non-default) setting is overriding that.
This is wonderful! Thank you 🙂
thanks, i prefer an explicit date