As your project becomes more complex and includes more branches and features, you will probably use the commit command more often to save iterations of your work. You will then want to find a way to keep track of all of these features and commits, understanding the changes adopted with each one. Put simply, the git log command helps you do just that.
As a default, this command, entered without any arguments, will simply list the commits that have been made in reverse chronological order, with the most recent being first.
While this is undoubtedly useful, you are also able to introduce a number of options or arguments to gain a more precise picture of the commit history and more quickly find the information you are looking for.
Some useful options of Git Log
The ‘author’ option allows you to search only for commits by a specific author. Simply adding –author=john for example, will return the commits carried out by John. In some instances you might want to find commits that have taken place within a certain date range. To do this you can use the –since, –before, –after and –until commands, or combinations of these, to see the commits from certain periods.
If you are looking for the specific differences introduced in a commit, the -p is another useful option. You can also limit the number of commits that are displayed to view changes in a smaller range of commits by adding a numerical value. The -p -2 combination, for example, will only show the changes in the last two commits.
Searching closely within the commit history
There are many options that can greatly increase the granularity of your searches and bring you quickly to the information you are looking for. The -S command is particularly useful in this regard. It is used to look for specific strings or text and searches through the diffs of each commit to find the matching text. This can be helpful when trying to find when a certain function or variable has been introduced. Where you are looking for a specific phrase in the commit message, the –grep option will take you to the relevant text.
Git Log: a simple but powerful tool
The above is by no means an exhaustive list of git log options but it does give a flavour of how the command is used and of the useful data it can provide. Its operation is simple but it is also a powerful, time-saving tool. It can be thought of as an initial command, which is then qualified by the use of options. Where combinations of these options are used, it can produce extremely precise results quickly and easily.
Read more about Git in the following post: Git for Dummies.