Git log examples and nodejs scripts
The git log command can be used to log out a list of comment subject messages, commit hash numbers, and A wide range of other things about each commit in a git folder. It is a useful little command with many format options that can be useful when it comes to writing some kind of script that loops over all commits in a repository. There is just using the command by itself in the command line, and then there is piping it to something else, or better yet making a node.js script that uses it via the spawn method in the child process module. In this post I will be going over some quick examples of doing bolth.
1 - git log commend line examples
So in the section I will be starting out with some command line examples of the git log command. Because we are just logging information about commits there should not be any risk of messing up a git folder, but do not just take my word for it always take a look at the manual pages of a command that you are not familiar with, and always make sure your data is backed up if you are new to git.
1.1 - Basic git log example
So for starters there is just using the git log command in the command line by itself, by just typing git log in the command line you end up with the default result of doing so in a git folder.
This will result in all of the commits being displayed with a default format that includes the command hash number the author of the commit the date of the commit and then then subject message. However there will come a time in which only a certain number of commits should be logged and with a custom format. So that being said there is of course a whole lot of options that can be used to do just that, I am not going to go over all of them of course. However I will be taking a quick look at some of them, including how to make a custom format for the use in a node.js script. However lets look at some more simple command line only examples before getting to that.
1.2 - Limiting the number of commits and custom formating
So here I have an example that limits the number of commits to 3 and also makes use of custom formating.
1.3 - Limiting from a given date and time
The –since option can be used to limit from a given date and time forward. When it comes to setting the date and time a range of options can be used for the format of the date and time. This includes the default kind of format that is used when using the git log command without any options.
2 - Basic git log node.js script that logs subject names
So now for some node.js script examples that make use of the git log command via the spawn child process module. If you are not familiar with spawn it is a way to launch external commands on the host operating system via a node.js script. I will nit be geiig into spawn in detail here, but this will be a use case example of that native node.js method.
3 - Filtering
Now that I have the basic idea of how to go about making use of the git log command to create an array of git commits there is now filtering those commits to get a list of commits that meet a given criteria.