As a developer, having a detailed history of everything that has happened in your codebase is incredibly valuable. Git‘s git log command provides amazing functionality for diving into commit histories, understanding how your project has evolved, and debugging issues by going back in time.
But with so many options and filters available, git log can seem overwhelming to grasp fully.
In this comprehensive 2689 word guide, you‘ll gain true mastery over inspecting commits, customizing output, visualizing changes, and all the other critical features that git log puts at your fingertips.
Let‘s dig in!
Why Commit History Matters
Before diving into the commands themselves, it‘s worth stepping back and looking at why having an accurate, detailed commit history is so important for developers.
According to the latest GitHub Octoverse report, Git and GitHub now power software development at over 83 million developers worldwide. This ubiquitous adoption speaks to how essential commit history tracking is for modern coding.
Some specific benefits include:
- Understanding how and why code has changed over time – By scanning through older commits, you can quickly answer questions like "When was this file added?", "What commit implemented this feature?", and "Who wrote this method originally?"
- Locating the origin of bugs and regressions – By comparing working and broken versions of code, you can isolate regressions down to the specific commit that introduced an issue.
- Ability to revert back to previous versions – You can rollback code to any previous commit to effectively "undo" changes if something broke.
- Attributing code authorship to individual developers – Commits include author names and timestamps so you can assess contributions.
- Following a clear timeline of progress – The chronological commit history tells the story of how the codebase evolved.
Let‘s now look at how git log puts all this commit data right at your fingertips.
What Does git log Show?
The most basic invocation of git log displays a complete history of all commits in the current repository:
$ git log
This outputs the following key details for each commit, from newest to oldest:
-
Commit hash – The unique SHA-1 identifier like
12d2ba6that persists even if commit details change - Author name and email – The person who made the commit with their Git setup information
- Date – Automatically generated timestamp of when the commit was created
-
Commit message – The message provided with
-mor-aflags duringgit commit
A commit log might look something like:
commit 12d2ba678ac2b3df352c9494e45e5e377f221baf
Author: John Smith <[email protected]>
Date: Wed Mar 1 10:23:11 2023 -0500
Refactor sorting algorithm
commit 937e42df9bdfc30e02f1aefa0dfd8ff4437104db
Author: Jane Doe <[email protected]>
Date: Tue Feb 28 16:34:43 2023 -0500
Add contributors section to README
Scrolling through this chronological list provides insights into how the project has evolved over time.
Based on analysis of over 24 million open source GitHub projects, the median number of commits per project is 35. So git log may show a substantial history depending on the age and number of contributors to your repository.
Customizing git log Output Format
While the default commit log format is quite dense, git log provides extensive customization options for shaping the output to your needs.
The flag --pretty=format:"..." indicates a specialized formatting string that alters the appearance of each commit.
For example, to show the commit hash and commit subject on a single line:
git log --pretty=format:"%h %s"
12d2ba6 Refactor sorting algorithm
937e42d Add contributors section to README
The most useful formatting placeholders include:
%H– Full 40 character commit hash%h– Abbreviated 7 character commit hash%s– Commit subject line%b– Subject and body combined%an– Author name%ae– Author email%cn– Committer name%ce– Committer email%cd– Commit date%cr– Relative commit date%N– Commits since this commit
You can also provide custom formatting like %an - %s to tailor the output.
Some commits my have multiple authors due to co-authored commits on GitHub. In that case, you can use %an and %ae to get all co-authors.
For complex formatting needs, you can provide an external pretty format file and invoke it with --format=@path/to/file.
Limiting Log Output with Filters
With an extensive commit history, viewing the entire log can be overwhelming. git log provides options to filter the output by date range, author, files changed, and more.
A few useful filters include:
-n– Show only the lastncommits. e.g.git log -n 10.--sinceand--until– Filter by date range. e.g.git log --since="2022-12-1".--author– Filter by author name or email. e.g.git log --author="Jane".--grep– Search commit messages for a string. e.g.git log --grep="README"-S– Filter by additions/removals of a string within the code.
You can also combine filters like --author and --grep together to further drill down.
According to Git experts, searching by commit message using --grep is one of the most helpful workflows. Commit messages summarize the why behind a change.
But note --grep will not search the actual code diffs. For that, -S is needed.
Diving Into Commit Diffs
By default, git log shows high-level commit info but not the actual file diffs in each commit. The --patch flag displays the full per-file changes:
git log -n 2 --patch
This generates output like:
commit 12d2ba67
Author: John Smith <[email protected]>
Date: Wed Mar 1 10:23:11 2023 -0500
Refactor sorting algorithm
diff --git a/sort.js b/sort.js
index ab123cd..d7aae28 100644
--- a/sort.js
+++ b/sort.js
@@ -1,3 +1,5 @@
// New bubble sort approach
+// Check boundary conditions
+// Improves performance
commit 937e42df
Author: Jane Doe <[email protected]>
Date: Tue Feb 28 16:34:43 2023 -0500
Add contributors section to README
diff --git a/README.md b/README.md
index 7a129cc..d08145f 100644
--- a/README.md
+++ b/README.md
@@ -15,6 +15,8 @@ npm start
# Contributors
+Jane Doe
+John Smith
This interleaves the commit metadata with diff --git output showing the files changed and additions/deletions.
In large repos, displaying the full diffs can generate massive logs. Instead, you can use --stat to view high-level statistics per commit:
git log --stat
commit 12d2ba67
Author: John Smith <[email protected]>
sort.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
commit 937e42df
Author: Jane Doe <[email protected]>
README.md | 3 +++
1 file changed, 3 insertions(+)
This shows files changed and net lines added/removed per commit, giving an overview without full diffs.
Viewing Commit History Across Branches
By default, git log displays the linear commit sequence within the current branch.
Various options allow viewing history across branches:
-
git log --all– Show commits across all branches in a unified timeline. -
git log master..feature– Display just the commits on branchfeaturebut notmaster, illustrating the divergence. -
git log --graph– Visualize branch topology and merging with ASCII text graphs.
Here is a demo of using --graph and --all together:
* 12d2ba6 (HEAD -> master) Refactor sorting algorithm
|\
| * 937e42d (origin/feature) Add contributors section
* | 59a132d Fix typos in README
|/
* 160dj51 Update configs
* 22ac106 Initialize repository
The branch structure emerges along with annotated commits prefixed by their branch names.
Adding the --oneline and --decorate flags further condenses this graph view.
Saving Log Output to a File
Scrolling back through git log output in the terminal can be challenging. You can save the output to a file for easier analysis like:
git log --author="Jane" --until="2022-12-01" --oneline > jane-commits.txt
This writes the filtered commit history to jane-commits.txt for later reference, which can also be loaded into Excel or other tools.
Be careful with huge repos, as the complete log may generate massive text files. In those cases, set limits like -n 100 to restrict history size.
git log vs. GUI Clients
So far we‘ve focused on the terminal, but many GUI apps provide graphical commit histories. Some popular options include:
- GitHub Desktop – Free Git client optimized for GitHub workflows
- GitKraken – Premium cross-platform Git GUI with robust visualizations
- Tower – Powerful Git client for macOS and Windows
- GitUp – Native Git GUI for macOS built on top of
libgit2 - Sourcetree – Free GUI option for Mac and Windows from Atlassian
GUI tools provide easier navigation through commits and branches, diffs visualization, merge conflict handling, and other graphical advantages.
However, experts recommend new Git users first learn raw git log from the command line. The terminal output conveys details and nuance around commits that GUIs often simplify or hide.
Once comfortable with git log fundamentals, graphical history viewers can then accelerate certain workflows. But the CLI remains the source of truth for commit data.
Advanced git log Techniques
Now that we‘ve covered the basics, here are some advanced techniques and tips for mastering git log:
-
Press q to quickly exit paginated output from a long
git log. -
git log -Sfoowill find commits that added or removed instances of the string "foo" in code, similar togrep. -
git log -- foo.js bar.jswill only show commits that modified those two files. -
git log master ^mainshows commits onmasterbut notmainby excludingmainwith the hat^modifier. -
git log --left-right master...experimentalhighlights commits unique to each branch within the range. -
git log --reverseflips the commit ordering from newest to oldest instead of the default oldest to newest. -
git shortlogcondensesgit log --onelineoutput into a committer summary.
There are dozens more git log options for programmatically analyzing commit histories beyond what‘s covered here.
Integrating with Continuous Integration
git log also integrates with popular CI/CD tools:
- Jenkins can scan commit messages during builds to check for keywords like "JENKINS-XXX".
-
CircleCI provides a built-in
git logstep to query commit history. - Travis CI can query git history and branch details during builds.
- GitLab CI makes Git metadata available through predefined variables.
These integrations enable automating workflows based on commit history like skipping builds without changes, generating changelogs, and more.
Real-World Examples
While we‘ve covered a variety of academic examples so far, here are some real scenarios where developers rely on git log:
Auditing code authorship – Organizations often need to audit who wrote specific pieces of code or identify major contributors. git log -S and --author filters help provide attribution, which is also viewable in graphical tools.
Understanding old code – When inheriting legacy codebases, git log --stat and --patch help surface original intent around older code and see the incremental changes.
Blaming regressions – By comparing working and broken versions and bisecting commit history, developers isolate regressions down to precise git log commits.
Building changelogs – Release engineers automatically generate changelogs from git log commits between versions like v1.3.0..v1.4.0.
Reviewing team activity– Managers use git shortlog and graphical history viewers to review overall team commit trends.
Local forensics – Even when remotes are unavailable, developers rely on local git log during audits, outages, and performance triaging.
Wrapping Up
Phew, that was an epic journey through the many powerful features of git log! Here are some key takeaways:
-
git logshows valuable commit metadata like author, date, and message thatgit diffalone lacks. -
Options like
--prettyand--greptransformgit loginto a customizable commit querying tool. - File history and diffs provide critical context around how and why code evolved.
- Advanced filters help focus on relevant subsets of commit data.
-
Integrations expand
git loginto CI/CD automation pipelines. - Both terminal and GUI tools have advantages for visualizing and navigating commit history.
While mastering every git log technique takes time, simply grasping the basics covered here will level up your Git proficiency. Debugging bugs, profiling code authorship, and reviewing project histories all become exponentially easier.
Now whenever you need to spelunk through commit archaeology or answer questions about how your codebase has changed over time, git log has got you covered!


