This command is a really cool one. Well for me it is a cool one. The reason I really like this one is because it provides a way to look back on all the available history of the repository. What I mean when I say available is what resides in the local
.git folder. There are times when having to do a shallow clone is required so only a small subset of the references will reside there. That can make it a little difficult to really look back on all of the history.
If you hadn’t guessed, the command I’m looking at this time around is the
git log command. This is one that I didn’t really think too much about when working with git. Part of that reason is because it’s one that I don’t use as much. It only really became useful when wanting to know more about a file or even a folder.
List commits that are reachable by following thehttps://git-scm.com/docs/git-log
parentlinks from the given commit(s), but exclude commits that are reachable from the one(s) given with a ^ in front of them. The output is given in reverse chronological order by default.
There are two parts from that which are key. The first, of course, being the reachable bit and the second being the reverse chronological order. What that means is commits will be shown with the latest on top and older ones on the bottom. The formatting is one thing we can also change for some better readability and in some cases more details. Those can be controlled by passing some options – or flags – when using the command line. One tool I really learned to like more and more as I used it.
One of the things I really learned when using the command was that it does base the log output on what is the current working branch. This is one of the things I hadn’t really thought about it until I had read the documentation.
I think one of my favorite things about the command is the ability to do a search for patterns in the commit history. Yes, it does do a search in commit messages which is why it really is important to use meaningful commit messages.
You can see from the image above I did a quick search for “keyboard,” on a repository I follow. That’s from QMK Firmware cloned down to my computer. But as you can probably tell there are a few extra characters added to that. If you don’t know it is the
\ that are extra as they are escape characters. It does use regular expressions so that is some really crazy, cool black magic that goes on.
There is quite a bit that goes on when the command is run but to be honest this command is, as I has mentioned earlier, not used a lot of the time. It does come in handy when you are trying to look for specific things that happened in the repo. A really good use case is looking for when merges happened. That can be done with:
git log --merges
That will list only the history of merge commits on the current working branch from newest to oldest. The reason I can think of listing those would be when a feature was either introduced or a fix was implemented. The great part about that being it will show the commit hash, who authored the commit, when it was committed, and of course the message used when it was committed.
The one thing I will end this post on is that you can also pass along a file or folder path to show the commit history. One thing I still to this day use when looking back on what was changed.