When working with Git, git log
is a powerful command showing the commit history.
The git log
command supports many options and parameters to control the output, allowing you, as a developer, to decide what information you want to see. Your preferences will depend on your specific needs, but let me show you my preferred git log
options.
I usually display one commit per line and use some color. Colors are helpful not only for aesthetics but also for highlighting useful information.
If you are using "git log" with no options, you will see something like this:
commit a5e4001cd88b3dd3ff374856514e20cfffe4379c (HEAD -> master, origin/master)
Author: Roberto B <your-email-address@domain.com>
Date: Fri Jun 19 07:48:04 2020 +0200
Clean up runtime parameters to improve readability.
For each commit, you will see more than one line with:
- the hash of the commit (the commit identifier), 40 characters long;
- the ref name (if the commit is related to a branch or tag or HEAD);
- The author with full name and email address;
- the date of the commit in a long format;
- the commit message.
1 commit - 1 line
If you want to see one commit per line, you need to use --pretty=oneline
option:
git log --pretty=oneline
In this case, you will see:
- the hash of the commit, 40 characters long;
- the ref name (if the commit is related to a branch or tag or HEAD);
- the commit message.
If 40 characters for the hash is too long, you can use the shorter values for the hash (but also unique) with --abbrev-commit
option:
git log --pretty=oneline --abbrev-commit
Is it better?
Let's customize the format
If you want to customize the output, you can use the format
option combined with --pretty
.
With --pretty=format
, you can use placeholders to specify which information you want to show.
For example:
git log --pretty=format:'%h %s'
You want to see:
-
%h
: abbreviated commit hash (7 characters, like--abbrev-commit
option); -
%s
: the subject of the comment of the commit.
More colors
With placeholders, you can also use colors (%C<colorname>
):
For example, like the example above, if you want to see the hash commit (%h
) with red color:
git log --pretty=format:'%Cred%h%Creset %s'
With %Cred
you want to use the red color. With %Creset
you want to reset the default color.
More colors, more information
Usually, I use this git log
configuration:
git log --pretty=format:'%Cred%h%Creset %C(bold blue)%an%Creset %Cgreen%cr%Creset %s%C(bold red)%d%Creset'
Where:
-
%Cred%h%Creset: the hash commit (
%h
) in red color; -
%C(bold blue)%an%Creset: the author name (
%an
) with bold blue color%C(bold blue)
; -
%Cgreen%cr%Creset: the committer date relative
%cr
(for example, relative means: "5 minutes ago") in green color%Cgreen
-
%s: the commit message (the subject with
%s
placeholder); -
%C(bold red)%d%Creset: the ref names
%d
in red bold color.
Now that you understand the basics, you can go to the official documentation at https://git-scm.com/docs/pretty-formats, start playing with all the placeholders, and customize your git log history.
Top comments (0)