Byte Size TCP: git log, Commit History Crumbs
Welcome to the first edition of Byte Size TCP (Tools, Concepts, and Projects). In this recurring segment, we'll spotlight a command, tool, or concept that can level up your tech game.
Today we are going to look at git log
, a powerful command that shows commit history and details changes to Git repositories.
The Tool: git log
What makes git log
so powerful is its filtering flexibility, formatting customization, and comprehensive repository history view, which can include other branches! Some of the filters most commonly used are --author
or --committer
flags that allow seeing what files were created or modified by a certain person. Time is also easily queryable via --since
or --until,
which makes timeframes of interest easy to scope, such as auditing recent activity or changes.
The Concept: View, Filter, and Search Commit History
Imagine you work collaboratively on an ADO wiki with your team. You noticed one of the SOP documents was recently changed, or you want to count how many documents you've published and shared with the team. Through the magic of git log, this information and more are just a few keystrokes away.
The Project: Let's Git it
If you were to open a terminal right now and execute the git log
command, you may notice a command not found or fatal error being returned. A set of conditions must be met to execute the command.
- The core Git CLI must be installed on the host.
- Mac:
brew install git
- Linux:
sudo apt upgrade | sudo apt install git
- Windows: visit
https://git-scm.com/download/win
- Mac:
- You must be inside a Git repository.
git log
requires access to the.git
folder inside of a repository directory to query the commit and associated metadata.
- The repository must contain at least one commit:
- The objective of
git log
is to show commit history, and without any history, the command will not work.
- The objective of
Let's take a look at some of the flags most likely to be used.
--author="john doe"
: This filter restricts the output to show only commits where the author (the person who originally wrote the code for the commit) matches "john doe". Note that this works on matching partial strings or if regular expression is preferred, it can be used.
--since="2024-01-01"
: This filter further narrows down the commits to only those that were made on or after January 1, 2024.
--format="%ad %s"
: This dynamic option customizes the output format of each commit.
%ad
: This placeholder represents the author date, which is the date the commit was originally authored.%s
: This placeholder represents the subject of the commit message (the first line of the commit message).
Example:
git log --author="john.doe@gmail.com" --since="2025-01-01" --format="%ad %s™
Output:
Wed Jan 22 14:45:40 2025 +0000 Added file1
Wed Jan 21 14:50:40 2025 +0000 Added file2
Wed Jan 20 14:55:40 2025 +0000 Updated file3
Note: The results are in reverse chronological order, with the most recent at the top. These can be combined with any other decorative or filtering command via standard pipe commands, such as wc -l
(word count).
Example:
git log --author="john.doe@gmail.com" --since="2025-01-01" --format="%ad %s™ | wc -l
Output:25
To get the commit history of a single file:
git log documents/myfile.md
Output:
commit 1ab0471z858c714e51c130a13793b1n1630a677d
Author: John Doe <john.doe@gmail. com>
Date: Wed Jan 22 14:45:40 2025 +0000
Updated myfile
commit 23n0471z808c7382ksc130a13793b1n1630a713m
Author: John Doe <john.doe@gmail. com>
Date: Wed Jan 25 17:45:40 2025 +0000
Added myfile
Extra Credit
While git log
shows you which commits touched a file, git blame
shows you the commit hash, author name, date and time, line number, and code line, in that order. This is incredibly useful for finding the author of a specific change.
git blame <path/to/your/file>
Output:
e4a1c9d2 (Alice Johnson 2024-10-01 14:23:45 -0500 1) import sys
7f3b29ad (Bob Smith 2024-10-03 09:14:12 -0500 2) import os
a1b2c3d4 (Charlie Davis 2024-11-10 16:01:33 -0500 3)
a1b2c3d4 (Charlie Davis 2024-11-10 16:01:33 -0500 4) def main():
c9d8e7f6 (Dana White 2024-12-05 12:44:55 -0500 5) print("Starting the program...")
e4a1c9d2 (Alice Johnson 2024-10-01 14:23:45 -0500 6) if len(sys.argv) > 1:
7f3b29ad (Bob Smith 2024-10-03 09:14:12 -0500 7) print(f"Arguments: {sys.argv[1:]}")
c9d8e7f6 (Dana White 2024-12-05 12:44:55 -0500 8) else:
c9d8e7f6 (Dana White 2024-12-05 12:44:55 -0500 9) print("No arguments provided.")
a1b2c3d4 (Charlie Davis 2024-11-10 16:01:33 -0500 10)
a1b2c3d4 (Charlie Davis 2024-11-10 16:01:33 -0500 11) if __name__ == "__main__":
e4a1c9d2 (Alice Johnson 2024-10-01 14:23:45 -0500 12) main()