created on | June 22, 2022 |
last modified on | October 27, 2023 |
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:
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.
which is the same as
which lists each commit with the SHA-1 hash abbreviated to the first seven bytes and the commit message:
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.
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:
For listing recent commits in all branches, I add the branch graph and branch names. The parameters for this log output are:
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.
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.
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):
Now, I can use the custom log with
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 :
This is how the log for looks like for a small test repo:
git Documentation, Reference: git-log - Show commit logs