I'm a big fan of git and specifically of using it on the command line. So, I thought I'd share a quick tip on the options I use for
git logto get a sense of a repository's history from the command line. There are a lot of options for
git log, and plenty of other people on the internet sharing what they use to spruce up
git log, but I thought I'd share what I do. Here's the command:
Let me break this down piece by piece.
git log --all --graph --decorate --date=local --color --pretty="%C(yellow) %h %C(green) %cn %C(blue) %cd %C(red) %d %Creset %s" | less -R
This makes all of the commits for all the references in
refs/show up. So any commit towards any of the branches, remotes, and tags that my repository knows about will show up.
A handy option to show an ASCII graph that gives you a visual sense of the commit history -- this makes it easy to see what branches were merged into what other branches.
Building off of the
--alloption, this prints out the ref names of any commits that are shown. That way, we know to which commit each branch, remote, and tag points to.
This shows the timestamp of each commit using my local timezone -- because I travel back and forth between the US and Uganda for my job at Fenix, this helps me keep my head straight when figuring out what commit happened when.
This colorizes the ASCII graph lines but is only needed if you're piping through
lesslike I am -- without using
less, the graph lines should be colorized without needing this option.
--pretty="%C(yellow) %h %C(green) %cn %C(blue) %cd %C(red) %d %Creset %s"
The most complicated part -- formatting the output with
--prettyto make it look nice. This line specifies where you want each part of the commit and in what color. Each time you want to call for a change in color, you do a
%C(color_name)and that will change the color for the following text. If you want to reset it to not be colorized anymore, you call
%Creset. Then, you also specify the attributes:
%his for the abbreviated commit hash,
%cnis for the committer name,
%cdis for the committer date,
%dis for the ref names with parentheses around them, and then
%sis the subject of the commit. So, my git log will read as follows: Change color to yellow, show the abbreviated commit hash, change color to green, show the date, change color to blue, show the committer name, change color to red, show any refs, reset the color to be none, and show the subject of the commit.
| less -R
Lastly, I pipe everything through the
lesscommand so that after I'm done with the command, no trace of it remains on the screen. I had to use the
-Roption to make it display correctly with less, but that may just be my own shell's problem. Here's what it looks like on DuckDuckGo's "zeroclickinfo-fathead" repository:
I'd also recommend creating an alias for this command so you don't have to memorize all this. I put an alias in my
~/.bash_profilefile like so:
alias glog='git log --all --graph --decorate --date=local --color --pretty="%C(yellow) %h %C(green) %cn %C(blue) %cd %C(red) %d %Creset %s" | less -R'
There are tons more options for
git logand you can read more about them in the source for git-log