Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Practical Git: Filter commit history with git log arguments


    In the last lesson, we learned how to format the git log output; in this lesson we will learn how to filter down to a specific set of commits. By default, git log shows every commit in a repo. We will walk through using a bunch of options to filter our git log commits to a more meaningful set (-n, --after, --before, --author, --grep, -S, -G, --no-merges, {ref}..{ref}, {files}). We will also show how all of the formatting and filtering options can be composed together to query exactly what you are looking for in your commit history.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    in our command line, we're inside of a directory called utility functions, which is a Git repository. When running the git log command, along with the formatting options like stat or one line, we can also pass an option that let us filter the commits that we see down to a smaller set.

    We can view a certain number of the most recent commits by passing in a number by itself. In this case, if we do three, it will show us the three most recent commits, or if we do two, it will only show us the two most recent commits. The after option will let us view only commits that have been made after a certain time like yesterday.

    If we run this command, we see only the commits that have happened between now and yesterday. Git uses a date partier called a proxy date, which will interpret many common values between these quotes. Along with yesterday, we could have also said 30 minutes ago or last Tuesday, or last week, or two weeks ago, or a specific date like March 15th, which we could also format like this.

    By default, Git uses the current time when the command was run as the end range for our time period, but we can also explicitly tell Git when to stop looking for commits by passing in the before option, which also accepts any approxidate like we used with after, so we can pass in yesterday. When we run this command, it will look for any commit that happened between March 15th and yesterday.

    One thing to know is that you may see the words since and until instead of after and before, and these are just aliases that Git treats the same way, so you can use whichever one you like. When using the git log command, we can filter down to a specific author by using the author option and passing in a string like Trevor, and this shows us all commits that have the word "Trevor" in the author name.

    Keep in mind that this also parses the email. Inside of these quotes of the author option, we can also use a regular expression, so we could for any author value which contain the word "Trevor" or "Jane." Now if we run that, we see author values that contain the word "Trevor" as well as "Jane."

    Along with searching for authors in our commits, we can search commit messages using the grep option, which works the same way as the author option where inside of these quotes we can pass a string or regular expression.

    For example, if we wanted to find the commit where we updated our copyright and we have decent commit messages, then we can grep for the word "copyright" in our commit messages, and it finds a commit that has the word "copyright" inside of its good message.

    We can also filter our log to commits that add or remove a specific piece of code. This is called a pickaxe. A pickaxe is different than the grep in author options because the string and regular expression versions are two separate options.

    To search for a string inside of our code changes, we use the capital S option followed immediately by quotes, so there is no equal sign between the option and the value as there normally is. Then inside of the quotes, we provide the value that we want to search for. Let's look for all commits that use the math object.

    If we run that, we see all the commits where the code changes include the word "math," but this isn't very helpful because it only gives us the normal commit log not the code changes itself. Let's go back to our command and when you use the pickaxe options, it's often helpful to provide the P for patch formatting option as well, so that when we rerun this, we now see the changes where the word "math" was either removed or introduced.

    Now going back to our command, if we want to use a regular expression for our search, we have to change this capital S into a capital G and we also need to remove the quotes. Now we can use a regular expression to search for the words "math" or "random." If we run that, now we see all the commits that have changes with the word "random" or "math."

    We often want to ignore the case of our searches in our filter options. We can do that by using the I for ignore flag, so now if we search for the author of "Jane," it will show us all the Jane's commits even though her name is not capitalized. If we didn't include the ignore case flag, then we wouldn't get any commits from Jane.

    We can use the ignore case option in other filter options. For example, if we wanted to do a string pickaxe search for the word "random" without the ignore case option, this will only find lower case versions of the word "random," like in this case right here. If we include the ignore case flag, then it will show us the upper case search results as well.

    By default when you run Git long, it will include merge commit such as this one. If you want to filter down to only the commits that are not merge commits, you can say "git log" and then use the no merges option.

    Now the commit that was previously showing above this commit no longer shows in our git log. We can also filter by commits that are contained within a range of references. For example, we could see where the master branch and the cool feature branch have diverged.

    The syntax is a reference, and then two dots, and then another reference. After we run the command, we see the commits that are contained between these two refs. By default the git log command will log commits from all tracked files.

    If we're only interested in its specific files, we can pass those to the git log command as arguments. For example, if we want to see all the commits that involve our license file, can say, "git log license dot MD," run that and it shows those commits. We can also pass in multiple options, so if we want to see the changes that happen inside of license and the readme, we can do that.

    All of the formatting and filtering options that we've talked about can be composed together with the git log command. For example, we could say that we want to see the last three commits by any author named Trevor, where the readme file was changed.

    For another example, let's say we want to view all of the commits that included the word "math" in the changes that happened between now and two months ago, and we want to show those in the condensed form with these statistics.