viewing git commit history

created onJune 22, 2022
last modified onOctober 27, 2023

git log

The commit history can be viewed with . The output contains the 40 byte SHA-1 hash, the author, commit date and time and the commit message for each commit:

commit 18004659bce79c5d63347a7acefd374d080f5870 Author: rudolf <rudolf@knusperfisch.de> Date: Tue Jun 21 09:24:57 2022 +0000 changed menu order in techdoc, app servers commit 750e7f8586d20801610095f853ec5349073c049a Author: rudolf <rudolf@knusperfisch.de> Date: Tue Jun 21 09:20:42 2022 +0000 replaced stocck photos in english version with local imgs ...

This format uses a lot of real estate in your terminal, but the git log output can be formatted in various ways. There are some predefined formats available, i.e.

git log --pretty=oneline --abbrev-commit

which is the same as

git log --oneline

which lists each commit with the SHA-1 hash abbreviated to the first seven bytes and the commit message:

1800465 changed menu order in techdoc, app servers 750e7f8 replaced stock photos in english version with local imgs

The abbreviated SHA-1 hash is enough to unambiguously reference a commit. You can even work with the SHA-1 hash of the commit abbreviated to less than the first seven bytes as long as the abbreviated SHA-1 hash is unique in your repo, which more often than not works with an SHA-1 hash abbreviated to the first four bytes.

customizing the log format

I prefer the commits for the current branch being listed with abbreviated SHA-1 hash, commit date and time, and commit message in the first line, followed by the parent commit hashes, author and committer on the next line. This output format makes good use of the terminal real estate. The parameters for this log output are:

--format='%Cblue%h%Creset %<(24,trunc)%cd %Cblue%s%Creset %nparent hashes: %C(#729fcf)%<(20,trunc)%p%Creset%<(10,trunc)author: %an %<(25,trunc)%ae %<(10,trunc)commiter: %cn %<(25,trunc)%ce%n' --date=format:'%a %Y-%m-%d %H:%M:%S'

For listing recent commits in all branches, I add the branch graph and branch names. The parameters for this log output are:

--all --graph --format='%Cblue%h%Creset %<(24,trunc)%cd %Cblue%s%Creset %nparent hashes: %C(#729fcf)%<(20,trunc)%p%Creset%<(10,trunc)author: %an %<(25,trunc)%ae %<(10,trunc)commiter: %cn %<(25,trunc)%ce%n%D%n' --date=format:'%a %Y-%m-%d %H:%M:%S'

The option works similar to the format for the C statement. For a newline, use instead of . The format specifiers are listed in the git log man page in section PRETTY FORMATS (run in your terminal). The format option honors the date formatting of the Unix/Linux command. is your friend here. Colors can be defined by using the standard terminal colors black, black, white, red, blue, green, yellow cyan, and magenta. To print just the string ‘whatever’ in yellow, use . If your terminal supports 24-bit RGB colors, you can define colors in 24-bit RGB hex notation, i.e. . 24-bit RGB hex notation works only if it is set in braces.

storing custom log formats

Of course, it makes sense to make the custom format available in a more convenient from than having to handle the whole format string every time you use the custom format. You can set a git alias or a bash alias to achive this.

setting an alias in .gitconfig

The custom log format can be set in the git config by defining an alias. Here, an alias cl (abbreviation for custom log) is defined (run this command as a user, not root):

git config --global alias.cl --format='%Cblue%h%Creset %<(24,trunc)%cd %Cblue%s%Creset %nparent hashes: %C(#729fcf)%<(20,trunc)%p%Creset%<(10,trunc)author: %an %<(25,trunc)%ae %<(10,trunc)commiter: %cn %<(25,trunc)%ce%n' --date=format:'%a %Y-%m-%d %H:%M:%S'
With the parameter, the config for all repos initialized or cloned by the user running the command is used. If you run the same command as root, the system-wide git config is used, which applies to all users.

Now, I can use the custom log with

setting an alias in .bashrc

The more often I use a command, the shorter I prefer it to be. For the custom git log formats, I added the aliases and (abbreviation for git log and git log all) to my :

alias gl="git log --format='%Cblue%h%Creset %cd %<(10,trunc)author: %an %<(10,trunc)commiter: %cn %Cblue%s%Creset %nparent hashes: %p%n' --date=format:'%Y-%m-%d %H:%M:%S'" alias gla="git log --all --graph --format='%Cblue%h%Creset %<(24,trunc)%cd %Cblue%s%Creset %nparent hashes: %C(#729fcf)%<(20,trunc)%p%Creset%<(10,trunc)author: %an %<(25,trunc)%ae %<(10,trunc)commiter: %cn %<(25,trunc)%ce%n%D%n' --date=format:'%a %Y-%m-%d %H:%M:%S'"

This is how the log for looks like for a small test repo:

click on image to enlarge

x

reference

git Documentation, Reference: git-log - Show commit logs

git Documentation, Reference: git-config - Get and set repository or global options