The git log
command is for exploring the history of a git
repository. You can use it to visualize, search and explore the unique story which has lead to the current state of the code. Personally, the most common ways I’ve used git log
are to review my last few commits and to see the commits I’ve made since a branch diverged from master.
git log master..HEAD
This is a one-dimensional and basic usage of git log
, and that has been perfectly fine for me. This is, quite frankly, all I have ever needed. Part of my journey though the git manuals is to re-discover familiar commands while looking for new ways to apply them in my workflow. I certainly learned a few things that I will be looking to employ as I see opportunities. I hope you’ll learn something from this review as well!
Basics
Visualize the whole network of changes
The log can all but replace the github network page, or fancy proprietary git GUI tools.
git log --all --graph
This is remarkably good for visualizing the basics of whose doing what, and where. I can imagine in large projects this view could get a bit unwieldy, in which case you’ll need to use it with some surgery. In normal size projects this is a good way to quickly get your bearings.
It’s worth a note that the --decorate
option which shows the full ref names for every commit is enabled by default. Ignoring the reasons for why one would want to turn them off, there is the --no-decorate
option which does allow for turning them off.
Hide the merge commits --no-merges
There are times when seeing how branches have been merged into each other is useful, but much of the time this is just noise. Filter out the noise:
git log --no-merges
Evolution of file(s)
You can easily see a list of commits which affected the given path(s).
git log ./README.md
It’s a bit trickier when a file has changed locations of course, but git log
has your back with the --follow
option which will follow file movements.
Tracking changes of line numbers or functions
You want to know how a certain section of a file changed.
git log -L 1,20:app.py
Show every commit, and diffs for them, which makes an alteration to the first 20 lines of a python file. Probably going to consist of changes that document every time you made new imports. :)
You can also look at commits which alter a particular function in a file.
git log -L :create_app:app.py
This will show all commits which affected the create_app
function. Extremely useful!
Basic output formatting --pretty
The above commands automatically shows the full diffs for each commit, but if you want to see a more concise summary of each commit the commit formatting and diff options of git log
are invaluable.
You can use -s
, --no-patch
to suppress the diff output entirely.
git log --no-patch -L :create_app:app.py
To format the commits, the --pretty
options are really convenient. The options in increasingly more verbose order are oneline
, short
, medium
, full
, fuller
, email
, raw
. You can also of course set your own formats.
git log --no-patch --pretty=oneline -L :create_app:app.py
Commit limiting
Along with being able to define ranges of commits using normal gitrevisions techniques, the git log
command offers additional ways to further limit the displayed objects.
Exploring by date with --since
and --until
Here’s one for the overzealous managers out there (not that you would ever read blogs about the git manual): every commit your dev team made in Q1 this year.
git log --since=2019-01-01 --until=2019-04-01 --no-merges
This shows them in reverse chronological order though, no executive is going to want to look at it like that. So we will flip them
git log --since=2019-01-01 --until=2019-04-01 --no-merges --reverse
These options are also available under the aliases --after
and --before
.
Exploring by --author
What has she ever done?
git log --author='Ada Lovelace'
Searching your log messages --grep
All the commits which contain the substring “separate”.
git log --grep=separate
Using some regex, let’s see only the ones that start with “separate”, but let’s also throw in the fact that we often don’t spell that word correctly.
git log --perl-regexp --regexp-ignore-case --grep='^sep[ae]rate\s'
Conclusion
I really enjoyed learning more about git log
and I hope that it prompts me to ask more interesting questions of my repositories. All the best! Thanks for reading the manual review!