Most developers using git have probably used the git log
command to get information on the history of a git repository. Many developers might have also used git log -- <path>
to get the history of a particular file. What was new to me, was that you can also use git log
to display only the specific information you require.
An example of a use case is to get all the dates on which changes have been made to a file (including when the file was introduced). You can use git log -- <path>
and parse the output, but there’s a much easier solution. You can use the command line option --pretty=<format>
instead, so you don’t have to do the parsing anymore. For the example of getting all the dates, it would look like git log --pretty=%cs -- <path>
. In this case %cs
represents the committer date.
Default output of git log
% git log -- scripts/compile_website.sh
commit 4725db827086d664ddb177dea68ba3873482cc46
Author: <author>
Date: Sun May 21 20:35:54 2023 +0200
Migrate generate website 'code' folder to 'generate-website-code'
commit 111733ee29a473cb3ddcbd0c3076ca716fd9ea9b
Author: <author>
Date: Sat Feb 25 12:00:54 2023 +0100
Migrate to YAML for defining files to process
commit 19a2c7fa6663836141016f6c491242e14015984e
Author: <author>
Date: Sun Feb 19 18:58:31 2023 +0100
Create compile website script
Output of git log
when only printing dates
% git log --pretty=%cs -- scripts/compile_website.sh
2023-05-21
2023-02-25
2023-02-19
Another program can much more easily parse this output.
Even more examples of playing with git log
can be found in this post on parsing git log.